Skip to content

TST_Mock.MockBuilder

Class

apex
global inherited sharing class TST_Mock.MockBuilder

Fluent builder wrapper that delegates to TST_Builder.Builder for record construction and auto-registers built records with TST_Mock.


Methods

MethodDescription
global SObject build()Builds a single mock record with a mock ID and registers it with TST_Mock for query interception.
global List<SObject> buildList()Builds a list of mock records with mock IDs and registers them with TST_Mock for query interception.
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count)Adds child records to the parent mock without field overrides.
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count, Map<SObjectField, Object> overrides)Adds child records to the parent mock using SObjectField token overrides.
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count, Map<String, Object> overrides)Adds child records to the parent mock using String field name overrides.
global TST_Mock.MockBuilder withChildren(String relationshipName, TST_Builder.Builder childBuilder)Adds child records using a pre-configured TST_Builder.Builder with an explicit relationship name.
global TST_Mock.MockBuilder withChildren(TST_Builder.Builder childBuilder)Adds child records using a pre-configured TST_Builder.Builder with auto-detected relationship name.
global TST_Mock.MockBuilder withCount(Integer count)Sets the number of mock records to build.
global TST_Mock.MockBuilder withCycle(SObjectField field, List<Object> values)Assigns a list of values that cycle across mock records built by buildList().
global TST_Mock.MockBuilder withCycle(String fieldName, List<Object> values)Assigns a list of values that cycle across mock records built by buildList().
global TST_Mock.MockBuilder withDefaultedField(SObjectField field)Specifies a single field to populate with a default value using an SObjectField token.
global TST_Mock.MockBuilder withDefaultedField(String fieldName)Specifies a single field to populate with a default value using a String name.
global TST_Mock.MockBuilder withDefaultedFields(List<Object> fields)Specifies fields to populate with default values, even if not required.
global TST_Mock.MockBuilder withOptionalField(SObjectField field)Specifies a field to treat as optional using an SObjectField token.
global TST_Mock.MockBuilder withOptionalField(String fieldName)Specifies a field to treat as optional using a String name.
global TST_Mock.MockBuilder withOptionalFields(List<Object> fields)Specifies fields to treat as optional, preventing auto-population.
global TST_Mock.MockBuilder withOverride(SObjectField field, Object value)Sets a single field value override for the mock records.
global TST_Mock.MockBuilder withOverride(String fieldName, Object value)Sets a single field value override using a String field name.
global TST_Mock.MockBuilder withOverrides(Map<SObjectField, Object> fieldOverrides)Sets field value overrides for the mock records.
global TST_Mock.MockBuilder withOverrides(Map<String, Object> overrides)Sets field value overrides using String field names.
global TST_Mock.MockBuilder withRecordType(String recordTypeDeveloperName)Sets the Record Type for the mock records using the Record Type's DeveloperName.

build

apex
global SObject build()

Builds a single mock record with a mock ID and registers it with TST_Mock for query interception.

Returns SObject — The built mock SObject record.

Example

apex
Foobar__c mock = (Foobar__c)TST_Mock.of(Foobar__c.SObjectType).build();

buildList

apex
global List<SObject> buildList()

Builds a list of mock records with mock IDs and registers them with TST_Mock for query interception.

Returns SObject — The list of built mock SObject records.

Example

apex
List<SObject> mocks = TST_Mock.of(Foobar__c.SObjectType)
    .withCount(3)
    .buildList();

withChildren

apex
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count)

Adds child records to the parent mock without field overrides.

Parameters

ParameterTypeDescription
childTypeSObjectTypeThe SObjectType of the child records.
countIntegerNumber of child records to create.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(Contact.SObjectType, 3)
    .build();
apex
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count, Map<SObjectField, Object> overrides)

Adds child records to the parent mock using SObjectField token overrides.

Parameters

ParameterTypeDescription
childTypeSObjectTypeThe SObjectType of the child records.
countIntegerNumber of child records to create.
overridesMapField overrides for child records using SObjectField tokens.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(Contact.SObjectType, 3, new Map<SObjectField, Object>{
        Contact.LastName => 'Doe'
    })
    .build();
apex
global TST_Mock.MockBuilder withChildren(SObjectType childType, Integer count, Map<String, Object> overrides)

Adds child records to the parent mock using String field name overrides.

Parameters

ParameterTypeDescription
childTypeSObjectTypeThe SObjectType of the child records.
countIntegerNumber of child records to create.
overridesMapField overrides for child records using String field names.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(Contact.SObjectType, 2, new Map<String, Object>{
        'LastName' => 'Smith'
    })
    .build();
apex
global TST_Mock.MockBuilder withChildren(String relationshipName, TST_Builder.Builder childBuilder)

Adds child records using a pre-configured TST_Builder.Builder with an explicit relationship name. Use when multiple relationships exist between the same parent and child types.

Parameters

ParameterTypeDescription
relationshipNameStringThe child relationship name (e.g., 'Contacts').
childBuilderTST_Builder.BuilderA pre-configured Builder instance for the child records.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
Foobar__c mock = (Foobar__c)TST_Mock.of(Foobar__c.SObjectType)
    .withChildren('PrimaryContacts__r',
        TST_Builder.of(Contact.SObjectType).withCount(2)
    )
    .build();
apex
global TST_Mock.MockBuilder withChildren(TST_Builder.Builder childBuilder)

Adds child records using a pre-configured TST_Builder.Builder with auto-detected relationship name.

Parameters

ParameterTypeDescription
childBuilderTST_Builder.BuilderA pre-configured Builder instance for the child records.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(
        TST_Builder.of(Contact.SObjectType)
            .withCount(2)
            .withOverride(Contact.LastName, 'Doe')
    )
    .build();

withCount

apex
global TST_Mock.MockBuilder withCount(Integer count)

Sets the number of mock records to build.

Parameters

ParameterTypeDescription
countIntegerThe number of records to create.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
List<SObject> mocks = TST_Mock.of(Foobar__c.SObjectType)
    .withCount(5)
    .buildList();

withCycle

apex
global TST_Mock.MockBuilder withCycle(SObjectField field, List<Object> values)

Assigns a list of values that cycle across mock records built by buildList(). When more records are built than values provided, the values repeat from the beginning.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token of the field to cycle.
valuesListThe list of values to cycle through. Must not be null or empty.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
List<SObject> mocks = TST_Mock.of(Foobar__c.SObjectType)
    .withCycle(Foobar__c.Picklist__c, new List<Object>{'Alpha', 'Beta', 'Gamma'})
    .withCount(6)
    .buildList();
apex
global TST_Mock.MockBuilder withCycle(String fieldName, List<Object> values)

Assigns a list of values that cycle across mock records built by buildList(). When more records are built than values provided, the values repeat from the beginning.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field to cycle.
valuesListThe list of values to cycle through. Must not be null or empty.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
List<SObject> mocks = TST_Mock.of(Foobar__c.SObjectType)
    .withCycle('Picklist__c', new List<Object>{'Alpha', 'Beta', 'Gamma'})
    .withCount(6)
    .buildList();

withDefaultedField

apex
global TST_Mock.MockBuilder withDefaultedField(SObjectField field)

Specifies a single field to populate with a default value using an SObjectField token.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token of the field.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withDefaultedField(Account.Description)
    .build();
apex
global TST_Mock.MockBuilder withDefaultedField(String fieldName)

Specifies a single field to populate with a default value using a String name.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Foobar__c.SObjectType)
    .withDefaultedField('Description')
    .build();

withDefaultedFields

apex
global TST_Mock.MockBuilder withDefaultedFields(List<Object> fields)

Specifies fields to populate with default values, even if not required.

Parameters

ParameterTypeDescription
fieldsListA list of fields (SObjectField tokens or String API names).

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withDefaultedFields(new List<Object>{ 'Description', Account.Industry })
    .build();

withOptionalField

apex
global TST_Mock.MockBuilder withOptionalField(SObjectField field)

Specifies a field to treat as optional using an SObjectField token.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token of the field to mark as optional.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withOptionalField(Account.Name)
    .build();
apex
global TST_Mock.MockBuilder withOptionalField(String fieldName)

Specifies a field to treat as optional using a String name.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field to mark as optional.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withOptionalField('Name')
    .build();

withOptionalFields

apex
global TST_Mock.MockBuilder withOptionalFields(List<Object> fields)

Specifies fields to treat as optional, preventing auto-population.

Parameters

ParameterTypeDescription
fieldsListA list of fields (SObjectField tokens or String API names).

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withOptionalFields(new List<Object>{ 'Name', Account.Type })
    .build();

withOverride

apex
global TST_Mock.MockBuilder withOverride(SObjectField field, Object value)

Sets a single field value override for the mock records.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObject field to override.
valueObjectThe value to set.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Foobar__c.SObjectType)
    .withOverride(Foobar__c.Name, 'Test')
    .build();
apex
global TST_Mock.MockBuilder withOverride(String fieldName, Object value)

Sets a single field value override using a String field name. Supports relationship traversal paths like 'Account.Name'.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field.
valueObjectThe value to set.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Foobar__c.SObjectType)
    .withOverride('Name', 'Test')
    .build();

withOverrides

apex
global TST_Mock.MockBuilder withOverrides(Map<SObjectField, Object> fieldOverrides)

Sets field value overrides for the mock records.

Parameters

ParameterTypeDescription
fieldOverridesMapMap of SObjectField to override values.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Foobar__c.SObjectType)
    .withOverrides(new Map<SObjectField, Object>{ Foobar__c.Name => 'Test' })
    .build();
apex
global TST_Mock.MockBuilder withOverrides(Map<String, Object> overrides)

Sets field value overrides using String field names. Supports relationship traversal paths like 'Account.Name'.

Parameters

ParameterTypeDescription
overridesMapMap of field API names to override values.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Foobar__c.SObjectType)
    .withOverrides(new Map<String, Object>{ 'Name' => 'Test', 'Account.Name' => 'Parent' })
    .build();

withRecordType

apex
global TST_Mock.MockBuilder withRecordType(String recordTypeDeveloperName)

Sets the Record Type for the mock records using the Record Type's DeveloperName.

Parameters

ParameterTypeDescription
recordTypeDeveloperNameStringThe DeveloperName of the Record Type.

Returns TST_Mock.MockBuilder — This MockBuilder instance for method chaining.

Example

apex
TST_Mock.of(Account.SObjectType)
    .withRecordType('Enterprise_Account')
    .build();