Does anybody know how to write testmethods for Duplication Rules?
I have created the Account Duplicate Rule with init conditions:
Current User: Profile not equal to System Administrator
with Alert and Report on Create.
Action on Create is Allow.
When the duplication appears, I have a choice (push Save button and save duplicated record). How to reflect this story in test method?
static void testMethod1() {
User u = createUser('Profile Name');
System.runAs(u) {
Account acc1 = createAccount();
insert acc1;
Account acc2 = createAccount();
Boolean isExceptionWasThrown = false;
try {
insert acc2;
}
catch(DmlException e) {
System.assertEquals(StatusCode.DUPLICATES_DETECTED, e.getDmlType(0));
System.assert(e.getMessage().contains('Message'));
isExceptionWasThrown = true;
}
System.assert(isExceptionWasThrown);
}
}
Flag isExceptionWasThrown was correctly set as true. Process is blocked in Alert Text (in Account Duplication Rule without creating related objects - Duplicate Record Set and Items) and I do not have a possibility to approve this duplication.