-
Notifications
You must be signed in to change notification settings - Fork 0
The MockSoql Class
This class extends the Soql class, and simulates queries made by Soql objects in @IsTest context when the framework is configured to use mocks.
The framework automatically returns a MockSoql instance whenever DatabaseLayer.Soql.newQuery is called after DatabaseLayer.useMocks() or DatabaseLayer.useMockSoql() is called.
Unlike Soql, MockSoql objects do not query the Salesforce database. Their queries return records based on custom logic injected by developers as part of the test.
Here is an example apex test that uses MockSoql:
@IsTest
static void someTest() {
DatabaseLayer.useMocks();
// Since useMocks was called, this should be a MockSoql object:
Soql accountQuery = DatabaseLayer.Soql.newQuery(Account.SObjectType);
// Inject some results that will be returned when the query runs:
Account acc = new MockRecord(Account.SObjectType)
?.withId()
?.toSObject();
MockSoql.setGlobalMock().withResults(new List<Account>{ acc });
Test.startTest();
List<Account> results = (List<Account>) accountQuery?.query();
Test.stopTest();
Assert.areEqual(1, results?.size(), 'Wrong # of Accounts returned');
Assert.areEqual(acc?.Id, results?.get(0)?.Id, 'Did not return mock account');
}💡 Important: MockSoql inherits all of the same methods as its parent Soql class, documented here. However, these methods do not interact with the Salesforce database.
This static method defines query-mocking logic to be used for all queries. To assign this logic to a specific query, use the instance method.
Use this method in conjunction with the MockSoql.StaticResults class for static query results, or use the MockSoql.Simulator interface to define your own custom logic.
static MockSoql.Simulator setGlobalMock(MockSoql.Simulator simulator)static MockSoql.StaticResults setGlobalMock()
DatabaseLayer.useMocks();
MockSoql.setGlobalMock()?.withResults(someRecords);This instance method defines query-mocking logic to be used for a specific query. To assign this logic to all queries, use the static method instead.
Use this method in conjunction with the MockSoql.StaticResults class for static query results, or use the MockSoql.Simulator interface to define your own custom logic.
MockSoql.Simulator setMock(MockSoql.Simulator simulator)MockSoql.StaticResults setMock()
DatabaseLayer.useMocks();
MockSoql mockQuery = (MockSoql) DatabaseLayer.Soql.newQuery(Account.SObjectType);
mockQuery?.setMock()?.withResults(someRecords);- Generating Test Records
- Dml
- Soql
- Cmdt
- Plugins
- DatabaseLayer
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- 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