-
Notifications
You must be signed in to change notification settings - Fork 0
Mocking Duplicate Rules
Using DatabaseLayer.Duplicates, you can easily engage mocks to test your code without directly interacting with Salesforce duplicate rules.
You can cover most test cases by adding just a few lines of code to your tests:
First, ensure all duplicate detection operations in the code path to be tested use DatabaseLayer.Duplicates methods. Standard Datacloud.FindDuplicates operations cannot be mocked.
In @IsTest context, call DatabaseLayer.useMocks() or DatabaseLayer.useMockDuplicates(). Now, any Duplicates operations will be processed by the MockDuplicates instead.
By default, MockDuplicates will return an empty result (no duplicates found). You can configure specific duplicate detection results using the MockDuplicates.BaseSimulator via the MockDuplicates.simulator property.
Finally, call the code you want to test. As it runs, the framework will run MockDuplicates logic instead of actually calling Salesforce duplicate detection APIs.
By default, MockDuplicates operations will simulate a successful duplicate detection operation with no duplicates found:
DatabaseLayer.useMocks();
Account account = new Account(Name = 'Acme Corp');
// By default, duplicate detection using mocks will succeed with no duplicates:
Duplicates.FindDuplicatesResult result = DatabaseLayer.Duplicates.findDuplicates(account);
Assert.isTrue(result?.isSuccess(), 'Detection failed');
Assert.areEqual(0, result?.getDuplicateResults()?.size(), 'Found unexpected duplicates');To configure specific duplicate detection results, use the MockDuplicates.BaseSimulator:
DatabaseLayer.useMocks();
MockDuplicates.simulator.withResults(Account.SObjectType).addRule().addMatch().addRecord();
Account account = new Account(Name = 'Acme Corp');
Duplicates.FindDuplicatesResult result = DatabaseLayer.Duplicates.findDuplicates(account);If you want duplicate detection to fail with an error, add errors to the result:
DatabaseLayer.useMocks();
MockDuplicates.simulator.withResults(Account.SObjectType).addError();
Account account = new Account();
Duplicates.FindDuplicatesResult result = DatabaseLayer.Duplicates.findDuplicates(account);- 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