TST_Builder
Class · Group: Testing
global inherited sharing class TST_BuilderKnown Derived Types: UTIL_SObjectBuilderDefaultProvider
An advanced factory for creating and inserting SObject records for Apex tests. This class provides a flexible way to generate test data, automatically handling required fields and complex object relationships using a fluid builder pattern.
Since: 1.0
Example:
Account account = (Account)TST_Builder.of(Account.SObjectType).build();
Account inMemory = (Account)TST_Builder.of(Account.SObjectType).withoutInsertion().build();
Account custom = (Account)TST_Builder.of(Account.SObjectType)
.withOverride(Account.Name, 'Acme Corp').build();
List<SObject> accounts = TST_Builder.of(Account.SObjectType).withCount(5).buildList();See Also: UTIL_SObjectBuilderDefaultProvider
Properties
| Property | Description |
|---|---|
| global static TST_Builder.DefaultFieldValueProvider autoDefaultFieldValueProvider | A special marker value that signals the factory to generate a default value for a field, even if it's not required. |
| global static TST_Builder.DefaultValueProvider defaultValueProvider | Overrides the default value provider instance with a custom implementation. |
| global static List optionalFields | A static list of fields to treat as optional for the current transaction's build operations. |
Methods
| Method | Description |
|---|---|
| global static TST_Builder.Builder of(SObjectType objectType) | Starts a new SObject build operation for the specified SObjectType. |
| global static TST_Builder.Builder of(String sObjectName) | Starts a new SObject build operation for the specified SObject API name. |
Inner Classes
| Class | Description |
|---|---|
| Builder | A fluid builder for configuring and creating SObject records. |
| DefaultFieldValueProvider | Base class for field-level default value providers. |
| DefaultValueProvider | Base class for default value providers. |
Property Details
autoDefaultFieldValueProvider
global static TST_Builder.DefaultFieldValueProvider autoDefaultFieldValueProviderType: TST_Builder.DefaultFieldValueProvider
A special marker value that signals the factory to generate a default value for a field, even if it's not required. Use this in field override maps to request automatic value generation.
Since:
Example:
defaultValueProvider
global static TST_Builder.DefaultValueProvider defaultValueProviderType: TST_Builder.DefaultValueProvider
Overrides the default value provider instance with a custom implementation. This allows developers to define custom logic for generating default field values. To customize, extend 'UTIL_SObjectBuilderDefaultProvider' and assign an instance here.
Since:
Example:
optionalFields
global static List<Object> optionalFieldsType: List
A static list of fields to treat as optional for the current transaction's build operations. Fields added to this list will not be automatically populated with default values, even if they are required.
Since:
Example:
Method Details
of
global static TST_Builder.Builder of(SObjectType objectType)Starts a new SObject build operation for the specified SObjectType.
Parameters:
objectType(SObjectType) - The SObjectType of the SObject to create.
Returns: TST_Builder.Builder - A new Builder instance to configure and execute the build.
Since: 1.0
Example:
// Build a single Account in memory with a specific name
Account account = (Account)TST_Builder.of(Account.SObjectType)
.withOverrides(new Map<String, Object>{ 'Name' => 'Test Account' })
.withoutInsertion()
.build();of
global static TST_Builder.Builder of(String sObjectName)Starts a new SObject build operation for the specified SObject API name.
Parameters:
sObjectName(String) - The API name of the SObject to create.
Returns: TST_Builder.Builder - A new Builder instance to configure and execute the build.
Since: 1.0
Example:
// Build and insert a list of 5 Foobars
List<SObject> fooBars = TST_Builder.of('Foobar__c')
.withCount(5)
.buildList();