Skip to content

The MockDml.Database Class

Jason Siders edited this page Jul 4, 2025 · 7 revisions

Simulates a Salesforce database when mocks are used. The class stores a MockDml.History object for each DML method, along with logic to handle savepoint/rollback behavior.

This object is available as a public static property, MockDml.MockDatabase. A blank database object is initialized by default. As records are submitted for mock DML over time, the records are then added to the appropriate history object.

Example:

DatabaseLayer.useMockDml();
Account acc = new Account();

Test.startTest();
DatabaseLayer.insert(acc);
Test.stopTest();

// Access the MockDml.Database's 'inserted' property:
Assert.isTrue(MockDml.MockDatabase.inserted?.wasProcessed(acc), 'Account was not inserted');
// This is functionally equivalent; INSERTED getter refers to the current MockDatabase:
Assert.isTrue(MockDml.INSERTED?.wasProcessed(acc), 'Account was not inserted');

Properties

Property Name Data Type Details
converted MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.CONVERTED getter property returns this value from the current mock database.
deleted MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.DELETED getter property returns this value from the current mock database.
inserted MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.INSERTED getter property returns this value from the current mock database.
published MockDml.PlatformEventHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.PUBLISHED getter property returns this value from the current mock database.
purged MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.PURGED getter property returns this value from the current mock database.
undeleted MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.UNDELETED getter property returns this value from the current mock database.
updated MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.UPDATED getter property returns this value from the current mock database.
upserted MockDml.RecordHistory A read-only property containing a history object that stores all upserted records during a transaction. The MockDml.UPSERTED getter property returns this value from the current mock database.
resetOnRollback Boolean This property determines how the database will behave when a rollback occurs.

By default (true), savepoints will store a "snapshot" of the mock database at the time that they were initialized. Rolling back the savepoint will then cause the current MockDatabase to be replaced with that snapshot.

If set to false, the database will "ignore" rollbacks. You'll still be able to reference any records that were processed in the corresponding history object, even if they were rolled back. This may be desirable if you want to see what happened before the rollback occurred, or to improve performance in cases where this isn't needed.

Methods

snapshot

  • MockDml.Database snapshot()

This method returns a copy of the current MockDatabase, using JSON-serialization. Changes to this snapshot object will not mutate the database object that generated it, and vice-versa.

Example:

MockDml.Database databaseSnapshot = MockDml.MockDatabase.snapshot();
Clone this wiki locally