-
Notifications
You must be signed in to change notification settings - Fork 0
The MockRecord Class
Builder object creates an SObject for use in apex tests, without interacting with the Salesforce database.
This supports several functions that are otherwise impossible without manipulating and/or retrieving records from the database:
- Simulate record inserts - provisioning a record ID.
- Simulate parent and child relationships retrieved through SOQL
- Set read-only fields, including formula fields, and audit fields like
CreatedDate
DatabaseLayer.useMocks();
// Generate some mock data:
Account mockAccount = (Account) new MockRecord(Account.SObjectType)
?.setField(Account.SomeFormulaCheckbox__c, true)
?.withId()
?.toSObject();
Contact mockContact = (Contact) new MockRecord(Contact.SObjectType)
?.setField(Contact.CreatedDate, DateTime.now())
?.setLookup(Contact.AccountId, mockAccount)
?.withId()
?.toSObject();
// Inject this contact in mock query results:
MockSoql.setGlobalMock().withResults(new List<Contact>{ mockContact });
// This query should now return our mocked contact:
List<Contact> contacts = (List<Contact>) DatabaseLayer.Soql.newQuery(Contact.SObjectType)
?.addSelect(new Soql.ParentField(Contact.AccountId, Account.SomeFormulaCheckbox__c))
?.addSelect(Contact.CreatedDate)
?.addOrderBy(Contact.CreatedDate, Soql.SortDirection.DESCENDING)
?.setRowLimit(1)
?.toSoql();To create a new MockRecord, supply an existing SObject record as a starting point, or start from scratch with just the SObjectType to be created:
MockRecord(SObject existingRecord)MockRecord(SObjectType objectType)
| Property | Data Type | Details |
|---|---|---|
| Id | Id | Getter returns the current record Id, if set. |
Adds records to a related list of child records on the record, by the lookup field on the child object, or its corresponding Schema.ChildRelationship. If the related list does not yet exist, one is created first.
MockRecord addToRelatedList(Schema.ChildRelationship childRelationship, List<SObject> childRecords)MockRecord addToRelatedList(SObjectField lookupFieldOnChildObject, List<SObject> childRecords)MockRecord addToRelatedList(Schema.ChildRelationship childRelationship, List<MockRecord> childRecords)MockRecord addToRelatedList(SObjectField lookupFieldOnChildObject, List<MockRecord> childRecords)
Returns the value of a field defined in the MockRecord.
Object get(String parameterName)Object get(SObjectField field)
Returns the value of a lookup relationship defined in the mock record:
SObject getLookup(SObjectField lookupField)
Retrieves the value of a related list that matches a given child relationship.
List<SObject> getRelatedList(Schema.ChildRelationship relationship)List<SObject> getRelatedList(SObjectField lookupField)
Static method generates a unique record Id for use in database operations. Use this utility method outside of a MockRecord object context. If working with a MockRecord, use the withId method instead.
static Id initRecordId(SObjectType objectType)
Sets the value of a given field(s).
MockRecord setField(Map<String, Object> fieldValues)MockRecord setField(String fieldName, Object value)MockRecord setField(SObjectField field, Object value)
Sets a lookup relationship on the record, by its corresponding lookup field.
This populates both the Id and the relationship itself. Ex., Contact.AccountId and Contact.Account.Id.
MockRecord setLookup(SObjectField lookupField, SObject parentRecord)MockRecord setLookup(SObjectField lookupField, MockRecord parentRecord)
Sets a related list of child records on the record, by the lookup field on the child object, or its corresponding Schema.ChildRelationship.
MockRecord setRelatedList(Schema.ChildRelationship childRelationship, List<SObject> childRecords)MockRecord setRelatedList(SObjectField lookupFieldOnChildObject, List<SObject> childRecords)MockRecord setRelatedList(Schema.ChildRelationship childRelationship, List<MockRecord> childRecords)MockRecord setRelatedList(SObjectField lookupFieldOnChildObject, List<MockRecord> childRecords)
Builds the current record instance to an actual SObject value.
To maximize performance, avoid calling this method until your record is fully built. Calling this method will cause any related parent/child MockRecord instances to be converted to an SObject as well.
SObject toSObject()
Sets the Id field with a new, valid record Id that matches the SObjectType. If an Id is already set, its value will not be overwritten.
MockRecord withId()
- Generating Test Records
- Dml
- Soql
- Cmdt
- Duplicates
- Plugins
- DatabaseLayer
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- Duplicates
- MockDuplicates
- MockSoql
-
Soql
- Soql.AggregateResult
- Soql.Aggregation
- Soql.Binder
- Soql.Builder
- Soql.Condition
- Soql.ConditionalLogic
- Soql.Criteria
- Soql.Cursor
- Soql.Function
- Soql.InnerQuery
- Soql.InvalidParameterValueException
- Soql.LogicType
- Soql.NullOrder
- Soql.Operation
- Soql.Operator
- Soql.ParentField
- Soql.PreAndPostProcessor
- Soql.QueryLocator
- Soql.Request
- Soql.Scope
- Soql.Selectable
- Soql.SortDirection
- Soql.SortOrder
- Soql.Subquery
- Soql.TypeOf
- Soql.Usage
- Soql.WhenClause