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
Shubham SonarShubham Sonar 

test data factory class

I am using number of objects in my app which are inter-related to each other. Whereas the controllers have good frequency of methods around it. Every time i create a test class and test method for testing any controller method, i need to create new Records which are required for the execution of controller methods. Is there any way to create a class which provides me all types of required records. Something like a data factory class which can be used in every test class during test execution.
Best Answer chosen by Shubham Sonar
NagendraNagendra (Salesforce Developers) 
Hi Shubham,

Please find the concept of test data factory class in the below example which might help you to build the required class.

//service provider

public abstract class TestDataFactory {
    
      
    //Account creation
    public static List<Account> createAccounts( Integer numberOfAccounts) {
        List<Account> accounts = new List<Account>();
        for ( Integer i = 0 ; i < numberOfAccounts ; i++ ) {
            
            Account account = new Account( firstname = 'Test Account' + Math.random(), lastname = 'Account',
                    PersonEmail = 'noreplay@email.com',Home_Phone__c= '(415) 419-8873',PersonMailingStreet = '5353 W.Test Rd',
                    PersonMailingCity = 'Testdale', PersonMailingState = 'CA', PersonMailingPostalCode = '94803');
            accounts.add( account);

        }
        return accounts;

    }
}


// Consumer class

@isTest
private class TestDataFactoryTest {
    
    static Account account;
    
 
    //test for creating Accounts
    static testMethod void createAccountsTest() {
        
        account = TestDataFactory.createAccounts(1)[0];
        insert account;
        
        
        system.debug('Account Id'+account.id);
        account = [select firstname, lastname,personmailingpostalcode,personEmail from account where id = :account.id];
        system.assertEquals(account.lastname, 'Account');
        system.assertEquals(account.personmailingpostalcode, '94803');
        system.debug('Account Firstname'+ account.FirstName);
        

    }
}
Please let us know if this helps.

Regards,
Nagendra.
 

All Answers

Lucas Duque 9Lucas Duque 9
What you could do is create a class that is responsible for create the records of test, that is, a class as "FactoryObjectsTest" and in that class create all the methods for the records, and when you're testing just instance this class "FactoryObjectsTest" and call the specific method for create a type of record. What you think ? 

Pretty much what I suggested is what you yourself said in your question.
NagendraNagendra (Salesforce Developers) 
Hi Shubham,

Please find the concept of test data factory class in the below example which might help you to build the required class.

//service provider

public abstract class TestDataFactory {
    
      
    //Account creation
    public static List<Account> createAccounts( Integer numberOfAccounts) {
        List<Account> accounts = new List<Account>();
        for ( Integer i = 0 ; i < numberOfAccounts ; i++ ) {
            
            Account account = new Account( firstname = 'Test Account' + Math.random(), lastname = 'Account',
                    PersonEmail = 'noreplay@email.com',Home_Phone__c= '(415) 419-8873',PersonMailingStreet = '5353 W.Test Rd',
                    PersonMailingCity = 'Testdale', PersonMailingState = 'CA', PersonMailingPostalCode = '94803');
            accounts.add( account);

        }
        return accounts;

    }
}


// Consumer class

@isTest
private class TestDataFactoryTest {
    
    static Account account;
    
 
    //test for creating Accounts
    static testMethod void createAccountsTest() {
        
        account = TestDataFactory.createAccounts(1)[0];
        insert account;
        
        
        system.debug('Account Id'+account.id);
        account = [select firstname, lastname,personmailingpostalcode,personEmail from account where id = :account.id];
        system.assertEquals(account.lastname, 'Account');
        system.assertEquals(account.personmailingpostalcode, '94803');
        system.debug('Account Firstname'+ account.FirstName);
        

    }
}
Please let us know if this helps.

Regards,
Nagendra.
 
This was selected as the best answer