mirror of
https://github.com/forcedotcom/afv-library.git
synced 2026-07-30 11:43:26 +08:00
* @W-21623314 Adding Apex skill and Apex testing skill * @W-21623314 Adding Apex skill and Apex testing skill * @W-21623314 Adding Apex skill and Apex testing skill * @W-21623314 Adding Apex skill and Apex testing skill * @W-21623314 Adding Apex skill and Apex testing skill * @W-21623314 Adding Apex skill and Apex testing skill
2.4 KiB
2.4 KiB
TestDataFactory Patterns
For the base class template, see assets/test-data-factory-template.cls.
Design Rules
- Always accept a
doInsertflag — lets callers modify records before insert - Append loop index to all fields that participate in matching rules — prevents
DUPLICATES_DETECTEDerrors from active Duplicate Rules - Single-record methods delegate to bulk — e.g.,
createAccount(doInsert)callscreateAccounts(1, doInsert)[0] - Return created records — enables chaining and further manipulation
- Set all required fields — include fields enforced by validation rules, not just schema-required fields
Field Override Pattern
Allow callers to override default values without creating new factory methods:
public static Account createAccount(Map<String, Object> fieldOverrides, Boolean doInsert) {
Account acc = new Account(
Name = 'Test Account',
Industry = 'Technology'
);
for (String fieldName : fieldOverrides.keySet()) {
acc.put(fieldName, fieldOverrides.get(fieldName));
}
if (doInsert) insert acc;
return acc;
}
// Usage:
Account acc = TestDataFactory.createAccount(new Map<String, Object>{
'Name' => 'Custom Name',
'Industry' => 'Healthcare'
}, true);
Record Type Support
public static Account createAccountByRecordType(String recordTypeName, Boolean doInsert) {
Id recordTypeId = Schema.SObjectType.Account
.getRecordTypeInfosByDeveloperName()
.get(recordTypeName)
.getRecordTypeId();
Account acc = new Account(
Name = 'Test Account',
RecordTypeId = recordTypeId
);
if (doInsert) insert acc;
return acc;
}
Handling Duplicate Rules
When unique field values alone are not sufficient, use Database.insert() with a DuplicateRuleHeader:
public static List<Account> createAccountsAllowDuplicates(Integer count, Boolean doInsert) {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < count; i++) {
accounts.add(new Account(
Name = 'Test Account ' + i,
Phone = '555-000-' + String.valueOf(i).leftPad(4, '0')
));
}
if (doInsert) {
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
Database.insert(accounts, dml);
}
return accounts;
}