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.

Since: 1.0


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

Method Details

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.

Since: 1.0

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.

Since: 1.0

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:

  • childType (SObjectType) - The SObjectType of the child records.
  • count (Integer) - Number of child records to create.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(Contact.SObjectType, 3)
    .build();

withChildren

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:

  • childType (SObjectType) - The SObjectType of the child records.
  • count (Integer) - Number of child records to create.
  • overrides (Map) - Field overrides for child records using SObjectField tokens.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

apex
Account mock = (Account)TST_Mock.of(Account.SObjectType)
    .withChildren(Contact.SObjectType, 3, new Map<SObjectField, Object>{
        Contact.LastName => 'Doe'
    })
    .build();

withChildren

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:

  • childType (SObjectType) - The SObjectType of the child records.
  • count (Integer) - Number of child records to create.
  • overrides (Map) - Field overrides for child records using String field names.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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

withChildren

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:

  • relationshipName (String) - The child relationship name (e.g., 'Contacts').
  • childBuilder (TST_Builder.Builder) - A pre-configured Builder instance for the child records.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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

withChildren

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:

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • count (Integer) - The number of records to create.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • field (SObjectField) - The SObjectField token of the field to cycle.
  • values (List) - The list of values to cycle through. Must not be null or empty.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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

withCycle

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:

  • fieldName (String) - The API name of the field to cycle.
  • values (List) - The list of values to cycle through. Must not be null or empty.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

apex
TST_Mock.of(Account.SObjectType)
    .withDefaultedField(Account.Description)
    .build();

withDefaultedField

apex
global TST_Mock.MockBuilder withDefaultedField(String fieldName)

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

Parameters:

  • fieldName (String) - The API name of the field.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • fields (List) - A list of fields (SObjectField tokens or String API names).

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • field (SObjectField) - The SObjectField token of the field to mark as optional.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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

withOptionalField

apex
global TST_Mock.MockBuilder withOptionalField(String fieldName)

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

Parameters:

  • fieldName (String) - The API name of the field to mark as optional.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • fields (List) - A list of fields (SObjectField tokens or String API names).

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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

withOverride

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:

  • fieldName (String) - The API name of the field.
  • value (Object) - The value to set.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • fieldOverrides (Map) - Map of SObjectField to override values.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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

withOverrides

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:

  • overrides (Map) - Map of field API names to override values.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

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:

  • recordTypeDeveloperName (String) - The DeveloperName of the Record Type.

Returns: TST_Mock.MockBuilder - This MockBuilder instance for method chaining.

Since: 1.0

Example:

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