Salesforce Shots: Test Data Factory in Salesforce

TestDataFactory/TestUtility is a container that is an org-wide special apex test class & excluded from the organization code size limit. In addition, it’s annotated with @isTest.

Use case:

In test classes, we used to create multiple test data records (Account, Contact, Opportunity, etc) for the testing apex class. In this scenario, we can use the Utility test class to create test data & use the same data in all the test classes.


Test utility classes/TestDataFactory contains methods & called by test methods to perform tasks, such as “Create Test Data”. Here is an example of the Test Data Factory pattern.

public class TestDataFactory 
    public static User createUser() 
        Profile pf = [SELECT Id,Name FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        User usr = new User(
            Alias  = 'usralias',
            Email  = '',
            Emailencodingkey  = 'UTF-8',
            Lastname  = 'user_lastname',
            Languagelocalekey   = 'en_US',
            Localesidkey   = 'en_US',
            Profileid  =  pf.Id,
            Timezonesidkey  = 'America/Los_Angeles',
            Username =  Math.random() + '',
            CompanyName  = 'the company'
        insert usr;
        return usr;

public static User createCommunityUser(){
        String uniqueUserName = 'CommuntiyUser' + + '';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Intforce Partner Profile'];
        User u = new User(Alias = 'comut', Email='',
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
                          LocaleSidKey='en_US', ProfileId = p.Id,
        return u;

You can create the method based on your requirement. In the above example, we are creating two different users: one is the System admin user, We can call this method like:

public void createUserTest() {
    User SystemAdmin = TestDataFactory.createUser();

Second is create a partner community user, for calling this method we need to run as a System Admin, Like:

public void createUserTest() {
      User PartnerUser = TestDataFactory.createCommunityUser();


Perigeon Software is a software development firm. With a fresh perspective and dedicated attention to each client, we provide a complete IT solution globally. By defining, designing, and developing solutions tailored to meet our clients’ business objectives, we ensure that our clients get the maximum return on their investment and support them in tackling evolving business and technological issues. Our mission is to provide the best customer service and expertise using the most practical and robust web technologies/software technologies to satisfy our clients’ IT objectives and to provide them with the business and competitive advantage they needed to succeed.

To learn more about perigeon’s portfolio, visit:

To learn more about perigeon’s Salesforce capabilities, visit:

Drop us a mail at to discuss your salesforce requirement.