You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Unit test never fails with Smart Factory


Salesforce Unit test will fail, if we get a new change request to add a new “Required” field to an object and if the existing test classes create the same object. Besides, if we have created the same object’s test record in many classes, then we need to update all the classes before deployment.

To avoid this issue, we can use Smart Factory class provided by salesforce.

Scenario 1: Without using Smart Factory

Account has a required field ‘Account Name’. The following method is written for creating test data.

Public static testmethod void insertAccount() { 
             Account testAccount = new Account(Name='Test Account'); 
             Insert testAccount; 

Now, the business requirement is changed to add a new required field named ‘Phone’ to the Account object.

Here, all the existing test methods that create Account test data will fail. So, the developer needs to fix this type of issue whenever a new required field is implemented in any object.

To overcome this issue, we can refer the smart factory in our test class. Now, let us see the same scenario using smart factory.

 Scenario 2: Using Smart Factory

The ‘Smart Factory’ provides test data objects with all required fields and object lookups are pre populated.

The following method will create test Account with all required fields

Public static testmethod void insertAccount() { 
        Account testAccount = (Account)SmartFactory.createSObject(Account, false); 

Scenario 3: Creation of Child Records without using Smart Factory

Let us consider the following scenario.

Creating a Contact test data needs the following steps:

Step 1: Create an Account

Account testAccount = new Account (Name=’Test Account');

Step 2: Create a Contact

Contact testContact = new Contact(AccountId=testAccount .Id);  
Public static testmethod void createContact() { 
      Account testAccount = new Account(Name='Test Account'); 
      Insert testAccount; 
      Contact testContact = new Contact(AccountId = testAccount.Id); // with all required fields 

In the above example the parent objects need to be created first and then the Contact created with Account lookup.

Scenario 4: Creation of Child Records using Smart Factory

SmartFactory uses the describe metadata to populate all fields with data of right type. For lookup fields, it creates an appropriate object and then uses that object’s ID.

Public static testmethod void createContact() { 
       Contact testContact = (Contact)SmartFactory.createSObject('Contact', true); 

The second argument (true) toggles the cascade option– which populates any lookup fields on the object with their own newly created objects.


Smart Factory is a powerful way to create hierarchies of test data quickly and easily; so, you can focus on writing test classes and logic. You can get the smart factory package from the below link.