Skip to content

Instantly share code, notes, and snippets.

@mdhorine
Created August 15, 2014 10:13
Show Gist options
  • Save mdhorine/b720bd3dce1b6a03b2c7 to your computer and use it in GitHub Desktop.
Save mdhorine/b720bd3dce1b6a03b2c7 to your computer and use it in GitHub Desktop.
Test class for the Duplicate Trigger
@isTest
private class DuplicateTriggerTest {
@isTest static void testDuplicateTrigger() {
/* This test class creates 200 test contacts, creates a second list which is copy
copy of the first, and then inserts both into the database. The second insert
uses Database calls to get the IDs of the inserted contacts (which should all
be duplicates). Then we make a SOQL call to the database to get the duplicate
flag for those contacts and make sure it was set to true prior to insert.
This test provides 100% test coverage for the DuplicateCheckTrigger, and uses
bulk apex as well to ensure governor limits are not hit.
*/
// Create 200 test contacts
List<Contact> contactTestList = new List<Contact>();
for (Integer i=0; i<200; i++) {
Contact c = new Contact(
FirstName = 'Test',
LastName = 'Contact' + i,
Passport_ID__c = String.valueOf(Math.roundToLong(100000000 * Math.random()))
);
contactTestList.add(c);
}
// Start the test
Test.startTest();
// Create a deep clone list of the first list of accounts, to insert later
List<Contact> contactTestList2 = contactTestList.deepClone(false);
// Insert the first contact list
insert contactTestList;
// Insert the second contact list and get the IDs. For each successful addition
// to the database, get the ID for the contact and add it to a list for our query
Database.SaveResult[] srList = Database.insert(contactTestList2);
List<ID> contactIDs = new List<ID>();
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
ID contactID = sr.getId();
contactIDs.add(contactID);
}
}
// Make a SOQL query against our second set of contacts to get the duplicate flag
// and make sure the flag was correctly set to true.
for (Contact c: [SELECT Duplicate_Flag__c from CONTACT WHERE ID in :contactIDs]) {
System.assertEquals(TRUE, c.Duplicate_Flag__c);
}
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment