Skip to content

TST_Builder

Class · Group: Testing

apex
global inherited sharing class TST_Builder

Known 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.

Example

apex
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


Methods

MethodDescription
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.

of

apex
global static TST_Builder.Builder of(SObjectType objectType)

Starts a new SObject build operation for the specified SObjectType.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObjectType of the SObject to create.

Returns TST_Builder.Builder — A new Builder instance to configure and execute the build.

Example

apex
// 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();
apex
global static TST_Builder.Builder of(String sObjectName)

Starts a new SObject build operation for the specified SObject API name.

Parameters

ParameterTypeDescription
sObjectNameStringThe API name of the SObject to create.

Returns TST_Builder.Builder — A new Builder instance to configure and execute the build.

Example

apex
// Build and insert a list of 5 Foobars
List<SObject> fooBars = TST_Builder.of('Foobar__c')
	.withCount(5)
	.buildList();

Properties

PropertyDescription
global static TST_Builder.DefaultFieldValueProvider autoDefaultFieldValueProviderA 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 defaultValueProviderOverrides the default value provider instance with a custom implementation.
global static List<Object> optionalFieldsA static list of fields to treat as optional for the current transaction's build operations.

autoDefaultFieldValueProvider

apex
global static TST_Builder.DefaultFieldValueProvider autoDefaultFieldValueProvider

Type: 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.

defaultValueProvider

apex
global static TST_Builder.DefaultValueProvider defaultValueProvider

Type: 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.

optionalFields

apex
global static List<Object> optionalFields

Type: 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.

Inner Classes

ClassDescription
BuilderA fluid builder for configuring and creating SObject records.
DefaultFieldValueProviderBase class for field-level default value providers.
DefaultValueProviderBase class for default value providers.