· tutorials · 3 min read

Salesforce Testing with TestDataFactory - Create Better Test Data Faster

Streamline Salesforce test data creation with TestDataFactory. Automatically populate required fields and create related records with ease.

Creating valid test data is simplified when using the TestDataFactory package. Every Salesforce org needs 75% test code coverage, and every test class needs test data. This is where the TestDataFactory comes in. It is a simplified way of creating the necessary data for your test classes.

Why Use TestDataFactory?

But why not just insert test data using standard DML statements? TestDataFactory offers several advantages:

  • Automatic Population of Required Fields: Ensures all mandatory fields are filled without manual input.
  • Simplified Creation of Related Records: Easily create and link related records.

This free, open-source package, created by Ahmed Bensaad, also allows for setting any custom field data as needed.

Getting Started

To start using TestDataFactory, you can install the package through the UI or the command line. Once installed, you can easily make calls to the class in your Apex code.

Installation

You can install the TestDataFactory unlocked package using the command line:

sf package install --package 04t1n000002WsK5AAK -r -o subscribe

Alternatively, you can install it through the Salesforce UI.

Using TestDataFactory in Your Code

To create an opportunity and a related account, follow these steps:

  1. Call the createSObject Method: This method is part of the TestDataFactory class.
  2. Pass in the SObject Name: Specify the name of the SObject you want to insert.
  3. Pass in a Map of Field Names and Values: Provide a map where the keys are field names and the values are the field values.

Here’s an example of how you can create a Contact and a related Account:

Contact con = (Contact)TestDataFactory.createSObject('Contact', new Map<String,Object>{
  'Email' => '[email protected]',
  'Account.Description' => 'Text for the Description field on the Account'
});

Handling Custom Fields

If you have a custom field that needs a value, you can use TestDataFactory.DEFAULT_VALUE to assign a type-safe value for the field.

Benefits at a Glance

The TestDataFactory package offers several key benefits:

  • Efficiency: Drastically reduces the time needed to create test data.
  • Reliability: Automatically handles required fields and relationships, reducing the risk of errors.
  • Flexibility: Easily set custom field values as needed.

Example Use Case

Imagine you need to create test data for an Opportunity and its related Account. Here’s a step-by-step process using TestDataFactory:

  1. Define the Data: Create a map with the necessary field values.
  2. Create the SObject: Use the createSObject method to create the Opportunity.
  3. Cast the Output: Assign the output to a variable of type Opportunity.
Opportunity opp = (Opportunity)TestDataFactory.createSObject('Opportunity', new Map<String,Object>{
  'Name' => 'Test Opportunity',
  'StageName' => 'Prospecting',
  'CloseDate' => Date.today().addDays(30),
  'Account.Name' => 'Test Account'
});

Conclusion

The TestDataFactory package will streamline the way you write test classes in Salesforce. By simplifying the creation of test data and ensuring required fields are automatically populated, you can focus more on writing effective tests and less on the setup.

Get started today and see how this powerful tool can enhance your development workflow.

Need Our Help To Get Your Data Into Salesforce?

Join dozens of other companies by learning how you can get all your company's data in one place.

Back to Blog