Skip to content

Instantly share code, notes, and snippets.

@mbotos
Last active December 27, 2016 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbotos/922ede15da91bcf3c912 to your computer and use it in GitHub Desktop.
Save mbotos/922ede15da91bcf3c912 to your computer and use it in GitHub Desktop.
Salesforce Testing Best Practices for Contacts
@isTest public class Contact_Test {
@isTest static void insertContactSetsId() {
// prepare for test under separate limits
Contact testContact = createContact();
// run just the code under test
Test.startTest();
insert testContact;
Test.stopTest();
// get results and assert test conditions
// query only sees data created during test
Contact resultContact = [select Id from Contact limit 1];
System.assertNotEquals(null, resultContact.Id, 'Id not set on insert');
}
@isTest static void updateContactUpdatesLastName() {
Contact testContact = insertContact();
Test.startTest();
testContact.LastName = 'Barnes';
update testContact;
Test.stopTest();
Contact resultContact = [select LastName from Contact limit 1];
System.assertEquals(testContact.LastName, resultContact.LastName, 'Last Name not updated');
}
@isTest static void deleteContactRemovesContact() {
Contact testContact = insertContact();
Test.startTest();
delete testContact;
Test.stopTest();
list<Contact> resultContacts = [select Id from Contact];
System.assertEquals(0, resultContacts.size(), 'Contact not deleted');
}
@isTest static void undeleteContactSetsId() {
Contact testContact = insertContact();
delete testContact;
Test.startTest();
undelete testContact;
Test.stopTest();
Contact resultContact = [select Id from Contact limit 1];
System.assertNotEquals(null, resultContact.Id, 'Id not set on undelete');
}
// create test object and all dependencies
static Contact createContact() {
Account testAccount = insertAccount();
User testUser = insertUser();
return new Contact(LastName = 'Adams', Account = testAccount, Owner = testUser);
}
static Account insertAccount() {
Account testAccount = new Account(Name = 'Acme');
insert testAccount;
return testAccount;
}
static Contact insertContact() {
Contact testContact = createContact();
insert testContact;
return testContact;
}
static User insertUser() {
// select the standard Profile that always exists in Salesforce
// Profiles and other system data are always available to isolated tests
Profile testProfile = [select Id from Profile where Name = 'Standard User'];
// username must be unique across all Salesforce organizations
string uniqueUsername = 'cole' + string.valueOf(Datetime.now().getTime()) + '@test.com';
// create User with required profile and fields
User testUser = new User(alias = 'coletest', email = 'cole@test.com',
emailencodingkey = 'UTF-8', lastname = 'Cole',
languagelocalekey = 'en_US', localesidkey = 'en_US',
profileid = testProfile.Id,
timezonesidkey = 'America/New_York',
username = uniqueUsername);
insert testUser;
return testUser;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment