SFDC Test Factory

SFDC Test Factory. Auto Create Test Data.

Salesforce-Test-Factory

SObject factory that can be used in unit tests to create test data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@isTest
public class TestFactory {
public static SObject createSObject(SObject sObj) {
// Check what type of object we are creating and add any defaults that are needed.
String objectName = String.valueOf(sObj.getSObjectType());
// Construct the default values class. Salesforce doesn't allow '__' in class names
String defaultClassName = 'TestFactory.' + objectName.replaceAll('__c|__', '') + 'Defaults';
// If there is a class that exists for the default values, then use them
if (Type.forName(defaultClassName) != null) {
sObj = createSObject(sObj, defaultClassName);
}
return sObj;
}
public static SObject createSObject(SObject sObj, Boolean doInsert) {
SObject retObject = createSObject(sObj);
if (doInsert) {
insert retObject;
}
return retObject;
}
public static SObject createSObject(SObject sObj, String defaultClassName) {
// Create an instance of the defaults class so we can get the Map of field defaults
Type t = Type.forName(defaultClassName);
if (t == null) {
Throw new TestFactoryException('Invalid defaults class.');
}
FieldDefaults defaults = (FieldDefaults)t.newInstance();
addFieldDefaults(sObj, defaults.getFieldDefaults());
return sObj;
}
public static SObject createSObject(SObject sObj, String defaultClassName, Boolean doInsert) {
SObject retObject = createSObject(sObj, defaultClassName);
if (doInsert) {
insert retObject;
}
return retObject;
}
public static SObject[] createSObjectList(Sobject sObj, Integer numberOfObjects) {
return createSObjectList(sObj, numberOfObjects, (String)null);
}
public static SObject[] createSObjectList(SObject sObj, Integer numberOfObjects, Boolean doInsert) {
SObject[] retList = createSObjectList(sObj, numberOfObjects, (String)null);
if (doInsert) {
insert retList;
}
return retList;
}
public static SObject[] createSObjectList(SObject sObj, Integer numberOfObjects, String defaultClassName, Boolean doInsert) {
SObject[] retList = createSObjectList(sObj, numberOfObjects, defaultClassName);
if (doInsert) {
insert retList;
}
return retList;
}
public static SObject[] createSObjectList(Sobject sObj, Integer numberOfObjects, String defaultClassName) {
SObject[] sObjs = new SObject[] {};
SObject newObj;
// Get one copy of the object
if (defaultClassName == null) {
newObj = createSObject(sObj);
} else {
newObj = createSObject(sObj, defaultClassName);
}
// Get the name field for the object
String nameField = nameFieldMap.get(String.valueOf(sObj.getSObjectType()));
if (nameField == null) {
nameField = 'Name';
}
// Clone the object the number of times requested. Increment the name field so each record is unique
for (Integer i = 0; i < numberOfObjects; i++) {
SObject clonedSObj = newObj.clone(false, true);
clonedSObj.put(nameField, (String)clonedSObj.get(nameField) + ' ' + i);
sObjs.add(clonedSObj);
}
return sObjs;
}
private static void addFieldDefaults(SObject sObj, Map<String, Object> defaults) {
// Loop through the map of fields and if they are null on the object, fill them.
for (String field : defaults.keySet()) {
if (sObj.get(field) == null) {
sObj.put(field, defaults.get(field));
}
}
}
// When we create a list of SObjects, we need to
private static Map<String, String> nameFieldMap = new Map<String, String> {
'Contact' => 'LastName',
'Case' => 'Subject'
};
public class TestFactoryException extends Exception {}
// Use the FieldDefaults interface to set up values you want to default in for all objects.
public interface FieldDefaults {
Map<String, Object> getFieldDefaults();
}
// To specify defaults for objects, use the naming convention [ObjectName]Defaults.
// For custom objects, omit the __c from the Object Name
public class AccountDefaults implements FieldDefaults {
public Map<String, Object> getFieldDefaults() {
return new Map<String, Object> {
'Name' => 'Test Account'
};
}
}
public class ContactDefaults implements FieldDefaults {
public Map<String, Object> getFieldDefaults() {
return new Map<String, Object> {
'FirstName' => 'First',
'LastName' => 'Last'
};
}
}
public class OpportunityDefaults implements FieldDefaults {
public Map<String, Object> getFieldDefaults() {
return new Map<String, Object> {
'Name' => 'Test Opportunity',
'StageName' => 'Closed Won',
'CloseDate' => System.today()
};
}
}
public class CaseDefaults implements FieldDefaults {
public Map<String, Object> getFieldDefaults() {
return new Map<String, Object> {
'Subject' => 'Test Case'
};
}
}
}

usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// The TestFactory will pre-fill all the fields we typically need
Account a = (Account)TestFactory.createSObject(new Account());
insert a;
// You can also set values to be used. Any values set in the constructor will override the defaults
Opportunity o = (Opportunity)TestFactory.createSObject(new Opportunity(AccountId = a.Id));
// You can also specify a specific set of overrides for different scenarios
Account a = (Account)TestFactory.createSObject(new Account(), 'TestFactory.AccountDefaults');
// Finally, get a bunch of records for testing bulk
Account[] aList = (Account[])TestFactory.createSObjectList(new Account(), 200);
// You can optionally insert records as created like this:
// Note the final parameter of true.
Account a = (Account) TestFactory.createSObject(new Account(), true);
Contact c = (Contact) TestFactory.createSObject(new Contact(AccountID = a.Id), true);