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
SabrentSabrent 

Test Data Factory

Can someone please explain me the concept of Test Data Factory for creating unit test classes or point me to a documnetation/ reference material where I can learn about Test Data Factory class. 

 

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
SabrentSabrent
So here's a little example my co-worker passed on to me.

//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);
        

    }
}

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

 

When you create an Apex Controller, Class or Trigger have an automatic unit test generator that automatically creates all the unit tests for you. This would assist creating meaningful unit tests with 75% code coverage for your Apex and help speed up the development cycle.

 

for more detail follow the below link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SabrentSabrent

Thanks for the response.

 

The link is a best practice link, is there a document that explains or has an example of how to write a test factory class?

SabrentSabrent
So here's a little example my co-worker passed on to me.

//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);
        

    }
}

 

This was selected as the best answer
surenssurens

thak u so much rov

Praveen RajendranPraveen Rajendran
try this link
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_data?trailmix_creator_id=strailhead&trailmix_id=dreamforce-18-platform-developer-1-bootcamp
JThakkarJThakkar
The TestDataFactory class is a special type of class—it is a public class that is annotated with isTest and can be accessed only from a running test. Test utility classes contain methods that can be called by test methods to perform useful tasks, such as setting up test data. Test utility classes are excluded from the org’s code size limit.
You can also try this link.
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_data