Method | Supported signatures |
---|---|
createSObject |
* createSObject(String sObjectName) // create an sObject with all required fields auto-filled and insert all sObjects
* createSObject(String sObjectName, Boolean doInsert) // create an sObject with all required fields auto-filled and insert all sObjects if the doInsert = true
* createSObject(String sObjectName, Map<String,Object> mapValuesOverride) // create an sObject with all required fields auto-filled, assign the values defined in the mapValuesOverride and insert all sObjects if the doInsert = true
* createSObject(String sObjectName, Map<String,Object> mapValuesOverride, Boolean doInsert) // create an sObject with all required fields auto-filled, assign the values defined in the mapValuesOverride and insert all sObjects if the doInsert = true |
createSObjectList |
* createSObjectList(String sObjectName, Integer numberOfSObjects) // create a list of sObject with all required fields auto-filled and insert all sObjects
* createSObjectList(String sObjectName, Integer numberOfSObjects, Boolean doInsert) // create a list of sObject with all required fields auto-filled and insert all sObjects if the doInsert = true
* createSObjectList(String sObjectName, Map<String,Object> mapValuesOverride, Integer numberOfSObjects) // create a list of sObject with all required fields auto-filled, assign the values defined in the mapValuesOverride and insert all sObjects
* createSObjectList(String sObjectName, Map<String,Object> mapValuesOverride, Integer numberOfSObjects, Boolean doInsert) // create a list of sObject with all required fields auto-filled, assign the values defined in the mapValuesOverride and insert all sObjects if the doInsert = true |
Contact con = (Contact)TestDataFactory.createSObject('Contact');
You can assign values to the main sObject or any related sObject, all sObjects will be created with the required fields auto-filled
Contact con = (Contact)TestDataFactory.createSObject('Contact', new Map<String,Object>{
'FirstName' => 'Doe',
'LastName' => 'John',
'Account.Description' => 'Description of the related account',
'Account.Parent.Name' => 'Name of the parent Account'
});
You can auto-generate a value for a non required field by assigning the TestDataFactory.DEFAULT_VALUE to it, in the Map of values
Contact con = (Contact)TestDataFactory.createSObject('Contact', new Map<String,Object>{
'Description' => TestDataFactory.DEFAULT_VALUE,
'Account.Phone' => TestDataFactory.DEFAULT_VALUE
});
For example when creating a Contact you can force the Test Data Factory to create a related Account, even if an Account is not required
Contact con = (Contact)TestDataFactory.createSObject('Contact', new Map<String,Object>{
'AccountId' => TestDataFactory.DEFAULT_VALUE
});
You can provide an Id for a required related sObject, to force the use of that Id and prevent the instantiation of the related sObject
User u = (User)TestDataFactory.createSObject('User', new Map<String,Object>{
'ProfileId' => UserInfo.getProfileId()
});
You can provide a sub map of values for a related sObject
Contact con = (Contact)TestDataFactory.createSObject('Contact', new Map<String,Object>{
'Description' => 'Contact description',
'Account' => new Map<String,Object>{
'Name' => 'Account Name',
'Description' => 'Account Description'
}
});
Create & insert 10 contacts
List<Contact> conList = TestDataFactory.createSObjectList('Contact',10);
The following code creates 10 users with different usernames and nicknames
List<User> uList = TestDataFactory.createSObjectList('User', new Map<String,Object>{
'ProfileId' => UserInfo.getProfileId(),
'Username' => 'test{!index}@mytestdomain.developer',
'CommunityNickname' => 'test{!index}'
},10);
Create a list of 5 Account sObjects with different names and a same description
List<Account> accList = TestDataFactory.createSObjectList('Account', new Map<String,Object>{
'Name' => new List<String>{'Google','Amazon','Facebook','Apple','Microsoft'},
'Description' => 'Same description'
},5);
Create a list of 100 Account sObjects with list of 5 different names that will loop and a same description
List<Account> accList = TestDataFactory.createSObjectList('Account', new Map<String,Object>{
'Name' => new List<String>{'Google','Amazon','Facebook','Apple','Microsoft'},
'Description' => 'Same description'
},100);
Create a list of Acount sObjects and link them to a list of 10 Case sObjects
List<Account> accList = TestDataFactory.createSObjectList('Account', new Map<String,Object>{
'Description' => 'Account Description'
},10);
List<Case> caseList = TestDataFactory.createSObjectList('Case', new Map<String,Object>{
'Account' => AccList,
'Contact.Account' => AccList
},10);