function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NickDevNickDev 

Multiple steps in Utility Test Class?

Hi,

I am writing a Utility Test Class which can be called by many test methods.

Basically, the data I am creating as part of this has a lot of pre-requisit bits of data.

My question is, would you normally call one method inside each other method in the test class to generate each pre-requisit bit of data or would you just simply call every test method seperately from the main test class? - Thank you

Example one:
@isTest(seeAllData=false)
public class TestUtilityClass {

public Account CreateAccount() {
     Account acct = new Account();
     acct.Name = 'Joe Bloggs & Co';
     insert acct;
     return acct;
}

public Contact CreateContact() {
     Account acct = CreateAccount();
     Contact contact = new Contact();
     contact.FirstName = 'Joe';
     contact.LastName = 'Bloggs';
     contact.AccountId = acct.Id;
     insert contact;
     return contact;
}
Then you call call it by
 
TestUtilityClass tcUtil = new TestUtilityClass();

Contact person = tcUtil.CreateContact();


Example two:
@isTest(seeAllData=false)
public class TestUtilityClass {

public Account CreateAccount() {
     Account acct = new Account();
     acct.Name = 'Joe Bloggs & Co';
     insert acct;
     return acct;
}

public Contact CreateContact(Account acct) {
     Contact contact = new Contact();
     contact.FirstName = 'Joe';
     contact.LastName = 'Bloggs';
     contact.AccountId = acct.Id;
     insert contact;
     return contact;
}
Then you call call it by:
TestUtilityClass tcUtil = new TestUtilityClass();

Account acct = tcUtil.CreateAccount();
Contact person = tcUtil.CreateContact(acct);
Amit Singh 1Amit Singh 1
Yeah, you can simply call one class(Utility Class) method to other class. Many times we need to generate test data that is required for many test cases so we use test utility class.

Refer below links for more Info.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm
https://help.salesforce.com/articleView?id=Why-is-a-Test-class-evaluated-as-part-of-the-Organization-s-Code-Coverage&language=en_US&type=1

Let me know if this helps :)

Thanks!
Amit Singh.
NickDevNickDev
Great thank you so calling just the UtilityClass once, and have that do all the work sounds the best option.

No need to keep calling each method seperately... Thanks!