Unit test never fails with Smart Factory

Share on facebook
Share on twitter
Share on linkedin
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. 

Introduction:

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.

Conclusion: 

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.

Reference:  https://github.com/mbotos/SmartFactory-for-Force.com/tree/master/src/classes

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Mobile Responsive Testing

Mobile Responsive Testing is simply a Testing Process performed to make sure that your website or an app is working appropriately on all types of devices.
Whether it an app or website, they need to ensure that they are accessible from any device, anytime, anywhere.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.