Build
Build in-memory SObject records without database operations.
Methods
| Method | Description |
|---|---|
set(SObjectField, Object) | Set field value using field token |
set(String, Object) | Set field value (supports dot notation for parents) |
setChildren(String, List) | Set child relationship records |
setFakeId() | Generate and set a fake ID |
build() | Build single mock record |
build(Integer) | Build multiple mock records |
set (SObjectField)
Set a field value using the SObjectField token.
Signature
Mocker set(SObjectField field, Object value);Test Lib
Account acc = (Account) AccountTestModule.Mocker()
.set(Account.Name, 'Mock Account')
.set(Account.Industry, 'Technology')
.build();set (String)
Set a field value using a string field name. Supports dot notation for parent relationships.
Signature
Mocker set(String field, Object value);Simple Field
Account acc = (Account) AccountTestModule.Mocker()
.set('Name', 'Mock Account')
.set('Industry', 'Technology')
.build();Parent Relationship
Account acc = (Account) AccountTestModule.Mocker()
.set('Parent.Name', 'Parent Corporation')
.build();
// acc.Parent.Name == 'Parent Corporation'Deeply Nested
Contact con = (Contact) ContactTestModule.Mocker()
.set('Account.Parent.Name', 'Grandparent Corp')
.build();
// con.Account.Parent.Name == 'Grandparent Corp'Read-Only Fields
Mocker can set fields that are normally read-only in Apex.
Account acc = (Account) AccountTestModule.Mocker()
.setFakeId()
.set('CreatedDate', Datetime.newInstance(2025, 1, 15, 10, 30, 0))
.set('Owner.Name', 'System Admin')
.build();
Assert.areEqual('System Admin', acc.Owner.Name);TIP
Use dot notation to mock parent relationships. This is especially useful when testing code that accesses parent fields from query results.
setChildren
Set child relationship records on a mock parent.
Signature
Mocker setChildren(String relationship, List<SObject> children);Traditional Apex
// Cannot mock child relationships without database queries
Account acc = [SELECT Id, (SELECT Id, LastName FROM Contacts) FROM Account WHERE Id = :accId];Test Lib
List<Contact> contacts = (List<Contact>) ContactTestModule.Mocker()
.build(3);
Account acc = (Account) AccountTestModule.Mocker()
.setChildren('Contacts', contacts)
.build();
// acc.Contacts.size() == 3Multiple Child Relationships
List<Contact> contacts = (List<Contact>) ContactTestModule.Mocker().build(5);
List<Opportunity> opportunities = (List<Opportunity>) OpportunityTestModule.Mocker().build(3);
Account acc = (Account) AccountTestModule.Mocker()
.setFakeId()
.setChildren('Contacts', contacts)
.setChildren('Opportunities', opportunities)
.build();
Assert.areEqual(5, acc.Contacts.size());
Assert.areEqual(3, acc.Opportunities.size());setFakeId
Generate and set a fake ID for the mock record.
Signature
Mocker setFakeId();Traditional Apex
// Cannot create valid IDs without database insert
Account acc = new Account(Name = 'Test');
// acc.Id is nullTest Lib
Account acc = (Account) AccountTestModule.Mocker()
.setFakeId()
.build();
// acc.Id is a valid-looking ID like 001000000000001
Assert.isNotNull(acc.Id);
Assert.isTrue(String.valueOf(acc.Id).startsWith('001'));Unique IDs
Each call to setFakeId() generates a unique ID.
Account acc1 = (Account) AccountTestModule.Mocker().setFakeId().build();
Account acc2 = (Account) AccountTestModule.Mocker().setFakeId().build();
Assert.areNotEqual(acc1.Id, acc2.Id);TIP
Use fake IDs when testing code that requires record IDs but doesn't need actual database records, such as Map operations or trigger handlers.
build
Build a single mock record.
Signature
SObject build();Traditional Apex
Account acc = new Account(Name = 'Test Account', Industry = 'Technology');
// Cannot set read-only fields like CreatedDate, formulas, etc.Test Lib
Account acc = (Account) AccountTestModule.Mocker()
.set(Account.Name, 'Test Account')
.set(Account.Industry, 'Technology')
.build();
// No database operation - perfect for unit testsbuild (Multiple)
Build multiple mock records.
Signature
List<SObject> build(Integer amount);Traditional Apex
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 10; i++) {
accounts.add(new Account(Name = 'Account ' + i));
}Test Lib
List<SObject> accounts = AccountTestModule.Mocker()
.set(Account.Industry, 'Technology')
.build(10);When to Use Mocker
Use Mocker instead of Builder when:
- Testing pure business logic without database
- Testing calculations and transformations
- Mocking query results with relationships
- Need faster test execution (no DML overhead)
- Need to set read-only fields (CreatedDate, formulas)
Example: Testing Business Logic
@IsTest
static void testDiscountCalculation() {
Account acc = (Account) AccountTestModule.Mocker()
.set(Account.AnnualRevenue, 500000)
.set(Account.Type, 'Customer')
.build();
Decimal discount = PricingService.calculateDiscount(acc);
Assert.areEqual(0.15, discount);
}TIP
Mocker creates records using JSON serialization, which allows setting read-only fields that are normally impossible to set in Apex.
