-
Notifications
You must be signed in to change notification settings - Fork 0
The MockDml Class
This class extends the Dml class, and simulates operations performed Dml objects in @IsTest context when the framework is configured to use mocks.
The framework automatically uses to a MockDml instance for DML operations whenever DatabaseLayer.Dml is called after DatabaseLayer.useMocks() or DatabaseLayer.useMockDml() is called.
Unlike Dml, Dml objects do not interact with the Salesforce database. Their operations "simulates" a successful DML operation by default, though it's possible to configure DML failures.
Since DML operations do not interact with the Salesforce database, changes processed via MockDml cannot be retrieved from the Database using traditional SOQL queries. Instead, callers can refer to a "mock database" via static MockDml properties to access records that were inserted, updated, etc.
Here is an example apex test that uses MockDml:
@IsTest
static void someTest() {
DatabaseLayer.useMocks();
Account account = new Account();
Test.startTest();
DatabaseLayer.Dml.doInsert(account);
Test.stopTest();
// The Account wasn't actually inserted, but it appears to be!
Assert.isNotNull(account?.Id, 'Missing Account Id');
Assert.isTrue(MockDml.INSERTED.wasProcessed(account?.Id, 'Account was not inserted');
}| Property Name | Data Type | Details |
|---|---|---|
| CONVERTED | MockDml.History | History object that records all Database.LeadConvert objects converted via Dml.convertLead. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| DELETED | MockDml.History | History object that records all SObject records deleted via Dml.doDelete. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| FAILURES | List<MockDml.ConditionalFailure> | Contains logic that runs on each DML operation to determine if a record should result in a DML failure. |
| INSERTED | MockDml.History | History object that records all SObject records inserted via Dml.doInsert. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| MockDatabase | MockDml.Database | Simulates a Salesforce database; comprised of history objects, plus some special logic which simulates rollbacks. |
| PUBLISHED | MockDml.History | History object that records all platform events inserted via Dml.doPublished. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| PURGED | MockDml.History | History object that records all SObject records purged from the recycle bin, via Dml.emptyRecycleBin. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| SAVEPOINTS | MockDml.SavepointHistory | History object that records all System.Savepoint records generated via Dml.setSavepoint. |
| UNDELETED | MockDml.History | History object that records all SObject records undeleted via Dml.doUndelete. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| UPDATED | MockDml.History | History object that records all SObject records upserted via Dml.doUpsert. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
| UPSERTED | MockDml.History | History object that records all SObject records updated via Dml.doUpdate. Getter property which retrieves an object of the same name from the MockDml.MockDatabase. |
💡 Important: MockDml inherits all of the same methods as its parent Dml class, documented here. However, these methods do not interact with the database.
This static method the MockDml.Database to its original state. All MockDml.History objects will be empty.
static void eraseAllHistories()
DatabaseLayer.useMocks();
DatabaseLayer.doInsert(new Account());
// Reset the mock database:
MockDml.eraseAllHistories();
Assert.isTrue(MockDml.INSERTED.isEmpty());This static method causes all subsuquent DML operations to fail.
static void shouldFail()
DatabaseLayer.useMocks();
MockDml.shouldFail();
DatabaseLayer.doInsert(new Account());
// ! Exception thrown: System.DmlExceptionThis static method injects a MockDml.ConditionalFailure object, which determines if subsuquent DML operation(s) should fail.
static void shouldFailIf(MockDml.Simulator simulator)
DatabaseLayer.useMocks();
// Let's say this object fails whenever an Account is updated:
MockDml.ConditionalFailure logic = new SomeConditionalLogic();
MockDml.shouldFailIf(logic);
// Perform some DML:
Account account = new Account();
Contact contact = new Contact();
// This succeeds, since it's not a DML update:
DatabaseLayer.Dml.doInsert(account);
// This succeeds, since it's not an Account:
DatabaseLayer.Dml.doUpdate(contact);
// This fails, since it's an Account update:
DatabaseLayer.Dml.doUpdate(account);This static method clears the current list of MockDml.ConditionalLogic failures. All subsuquent DML operation(s) should succeed.
static void shouldSucceed()
DatabaseLayer.useMocks();
MockDml.shouldFail();
// This should fail:
Database.SaveResult result1 = DatabaseLayer.Dml.doInsert(new Account(), false);
// Now, remove the failure - this should succeed:
MockDml.shouldSucceed();
Database.SaveResult result2 = DatabaseLayer.Dml.doInsert(new Account(), false);- 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