Test Data Factory in Salesforce : Amit Chaudhary

Test Data Factory in Salesforce
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post

In this post we will talk about how to create Test Data Factory in Salesforce with some Test class best practices. Common test utility classes are public test classes that contain reusable code for test data creation.

Test Data Factory Example

The TestDataFactory/ testutility  class is a special type of class — It is a public class that is annotated with @isTest and and as such, are excluded from the organization code size limit and execute in test context and can be accessed only from a running test. Let see the use case of Test data factory in Salesforce.

Use case : What about if we need to create multiple test classes in Salesforce and in most of the test classes we need to create some common test data like Account,Contact and Opportunity etc ? 

In this use case we can create one Util test class to create the test data. In that case, we can use the same test data in all test classes.

Test Data Factory pattern

Test utility classes/TestDataFactory contain methods that can be called by test methods to perform useful tasks, such as setting up test data. Here is example of Test Data Factory pattern.

Step 1:- Create TestDataFactory class

public class TestDataFactory {
        public static Account createAccount(Boolean doInsert) {
                createAccount('testAccount', doInsert);
        public static Account createAccount(String name, Boolean doInsert) {
                Account acc = new Account(name = name);
                if(doInsert) {
                        insert acc;
                return acc;
        public static Contact createContact(Boolean doInsert) {
                Account acc = createAccount(true);
                return createContact(acc.id, true);
        public static Contact createContact(Id accountId, Boolean doInsert) {
                Contact c = new Contact(firstName = 'Apex', lastName = 'Hours', accountId = accountId);
                if(doInsert) {
                        insert c;
                return c;
        public static List<Opportunity> createOpportunity(Id accountId, Integer numOpps) {
                List<Opportunity> opps = new List<Opportunity>();
                for(Integer i = 1; i <= numOpps; i++) {
                        Opportunity opp = new Opportunity();
                        opp.name = 'Account ' + i;
                        opp.accountId = accountid;
                        opp.amount = 1000;
                        opp.closeDate = Date.today().addDays(5);
                        opp.stageName = 'Prospecting';
                return opps;

You can overload the method base on your requirement. In above example we create two different method to create contact record. One if you want to create contact record with account , In that case we can call createContact method like below

public void myUnitTest() {
    Account acc = TestDataFactory.createAccount(true);
    Contact cont = TestDataFactory.createContact(acc.id, true); 

if you want to create account and contact together call createContact method with one param like below

public void myUnitTest() {
    Contact cont = TestDataFactory.createContact(true);    
        // This method will create account and contact


Like that you can create your test data in test data factory in Salesforce.

The post Test Data Factory in Salesforce appeared first on Apex Hours.

June 14, 2022 at 06:30PM
Click here for more details...

The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.