Last active
October 6, 2018 16:15
-
-
Save gbutt/2b30c085deb799bb21e6028d529e3cdb to your computer and use it in GitHub Desktop.
Apex TestDataFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestDataFactory { | |
public class SObjectGenerator { | |
public Integer seed = 0; | |
private Map<String,Object> fields; | |
private SObjectType sobType; | |
public SObjectGenerator(SObjectType sobType, Map<String,Object> fields) { | |
this.fields = fields; | |
} | |
public void mergeFields(SObject sob, Map<String,Object> fields) { | |
for (String key : fields) { | |
sob.put(key, fields.get(key)); | |
} | |
} | |
public SObject generateSObject() { | |
seed++; | |
SObject sob = sobType.newSObject(); | |
mergeFields(sob, fields); | |
return sob; | |
} | |
} | |
// Account Generator | |
private static SObjectGenerator accountGenerator = new SObjectGenerator(Account.SObjectType, new Map<String,Object>{}); | |
public static generateAccount() { | |
Account newSob = accountGenerator.generateSObject(); | |
newSob.Name = 'Test Account ' + accountGenerator.seed; | |
return newSob; | |
} | |
public static Account createAccount(){ | |
return createAccount(new Map<String,Object>()); | |
} | |
public static Account createAccount(Map<String,Object> extraFields) { | |
Account newSob = generateAccount(); | |
accountGenerator.mergeFields(newSob, extraFields); | |
insert newSob; | |
return newSob; | |
} | |
// Contact Generator | |
private static SObjectGenerator contactGenerator = new SObjectGenerator(Contact.SObjectType, new Map<String,Object>{ | |
'FirstName' => 'Test', | |
'LastName' => 'Contact' | |
}); | |
public static generateContact() { | |
Contact newSob = contactGenerator.generateSObject(); | |
newSob.Email = 'test' + contactGenerator.seed + '@example.com'; | |
return newSob; | |
} | |
public static Contact createContact(){ | |
return createContact(new Map<String,Object>()); | |
} | |
public static Contact createContact(Map<String,Object> extraFields) { | |
Contact newSob = generateContact(); | |
if (extraFields.containsKey('Account')) { | |
Map<String,Object> acctFields = extraFields.remove('Account'); | |
Account acct = createAccount(acctFields); | |
newSob.AccountId = acct.Id; | |
} | |
contactGenerator.mergeFields(newSob, extraFields); | |
if (newSob.AccountId == null) { | |
newSob.AccountId = createAccount(); | |
} | |
insert newSob; | |
return newSob; | |
} | |
// User Generator | |
private static SObjectGenerator userGenerator = new SObjectGenerator(User.SObjectType, new Map<String,Object>{ | |
'FirstName' => 'Test', | |
'LastName' => 'User', | |
'EmailEncodingKey' => 'UTF-8', | |
'LanguageLocaleKey' => 'en_US', | |
'LocaleSidKey' => 'en_US', | |
'TimezoneSidKey' => 'America/Denver' | |
}); | |
public static generateUser() { | |
User newSob = userGenerator.generateSObject(); | |
String alias = 'test' + userGenerator.seed; | |
newSob.Email = alias + '@example.com'; | |
newSob.Username = newSob.Email; | |
newSob.CommunityNickname = alias; | |
newSob.Alias = alias; | |
return newSob; | |
} | |
public static User createUser(){ | |
return createUser(new Map<String,Object>()); | |
} | |
public static User createUser(Map<String,Object> extraFields) { | |
User newSob = generateUser(); | |
if (extraFields.containsKey('Contact')) { | |
Map<String,Object> contFields = extraFields.remove('Contact'); | |
Contact acct = createContact(contFields); | |
newSob.ContactId = acct.Id; | |
} | |
userGenerator.mergeFields(newSob, extraFields); | |
// avoids MIXED_DML | |
System.runAs(new User(Id = UserInfo.getUserId())) { | |
insert newSob; | |
} | |
return newSob; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
public class TestDataFactoryTest() { | |
@TestSetup | |
static void makeData(){ | |
// account shared by contact 1 and 2 | |
Account acct = TestDataFactory.createAccount(); | |
// contact 1 is generated but not inserted | |
Contact cont1 = TestDataFactory.generateContact(); | |
// assign the account id manually | |
cont1.AccountId = acct; | |
insert cont1; | |
// contact 2 is inserted | |
// provide the account id as an extra field | |
Contact cont2 = TestDataFactory.createContact(new Map<String,Object>{ | |
'AccountId' => acct.Id | |
}); | |
// contact 3 creates its own account | |
// provide extra account fields | |
Contact cont3 = TestDataFactory.createContact(new Map<String,Object>{ | |
'FirstName' => 'Charlie', | |
'LastName' => 'Brown', | |
'Account' => new Map<String,Object> { | |
'Website' => 'https://www.peanuts.com' | |
} | |
}); | |
// community user | |
Profile commUserProfile = [SELECT Id FROM Profile WHERE Name = 'Customer Community User']; | |
User commUser = TestDataFactory.createUser(new Map<String,Object>{ | |
'ContactId' => cont3.Id, | |
'ProfileId' => commUserProfile.Id | |
}); | |
// admin user | |
Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator']; | |
UserRole adminRole = [SELECT Id FROM UserRole WHERE Name = 'Admin']; | |
User adminUser = TestDataFactory.createUser(new Map<String,Object>{ | |
'ProfileId' => adminProfile.Id, | |
'UserRoleId' => adminRole.Id | |
}); | |
} | |
@IsTest | |
static void it_should_create_account() { | |
// create account | |
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account 1']; | |
System.assertNotEquals(null, acct.Id); | |
} | |
@IsTest | |
static void it_should_generate_contact() { | |
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account 1']; | |
Contact cont1 = [SELECT Id, FirstName, LastName, AccountId FROM Contact WHERE Email = 'test1@example.com']; | |
System.assertNotEquals(null, cont1.Id); | |
System.assertEquals(acct.Id, cont1.AccountId); | |
System.assertEquals('Test', cont1.FirstName); | |
System.assertEquals('Contact', cont1.LastName); | |
} | |
@IsTest | |
static void it_should_create_contact() { | |
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account 1']; | |
Contact cont2 = [SELECT Id, FirstName, LastName, AccountId FROM Contact WHERE Email = 'test2@example.com']; | |
System.assertNotEquals(null, cont2.Id); | |
System.assertEquals(acct.Id, cont2.AccountId); | |
System.assertEquals('Test', cont2.FirstName); | |
System.assertEquals('Contact', cont2.LastName); | |
} | |
@IsTest | |
static void it_should_create_contact_with_account() { | |
Account acct = [SELECT Id FROM Account WHERE Name = 'Test Account 1']; | |
Contact cont3 = [SELECT Id, FirstName, LastName, AccountId, Account.Website FROM Contact WHERE Email = 'test3@example.com']; | |
System.assertNotEquals(null, cont3.Id); | |
System.assertNotEquals(null, cont3.AccountId); | |
System.assertNotEquals(acct.Id, cont3.AccountId); | |
System.assertEquals('Charlie', cont3.FirstName); | |
System.assertEquals('Brown', cont3.LastName); | |
System.assertEquals('https://www.peanuts.com', cont3.Account.Website); | |
} | |
@IsTest | |
static void it_should_create_community_user() { | |
User commUser = [SELECT Id FROM User WHERE Username = 'test1@example.com']; | |
} | |
@IsTest | |
static void it_should_create_admin_user() { | |
User adminUser = [SELECT Id FROM User WHERE Username = 'test2@example.com']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment