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.

Since: 1.0

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


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 optionalFieldsA static list of fields to treat as optional for the current transaction's build operations.

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.

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.

Property Details

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.

Since:

Example:

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.

Since:

Example:

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.

Since:

Example:


Method Details

of

apex
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:

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();

of

apex
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:

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