Pros
- @testSetup method is the first to be executed in the class,
- Records that are created in test@testSetup method will available for all the other classes and will roll back at the end of the
each and every method execution,whatever the field updates record deletion etc.. will be initialized for the next method - The next executing test method gets access to the original unmodified state of those records.
- Test setup methods aren’t supported in @isTest(SeeAllData=true) annotation
- Though the multiple test setup methods are allowed in a test class, order of execution is not guaranteed.
- if a test class fails such as null pointer exception or a assertion failure, the entire test class fails, and no further tests in the class are executed
- If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.
Example;
@isTest
public class TestAccountCommon {
// setup test data for entire test methods in teh test class
@testSetup static void setup(){
List<Account> lstAccounts = new List<Account>();
for(integer i = 0;i<5; i++){
lstAccounts.add( new Account(
FirstName = 'Test'+i,
LastName = 'user',
PersonEmail = 'test'+i+'@.com',
));
}
insert lstAccounts;
}
// test method 1 which usues defined test data
@isTest static void Test_TestAccount_001() {
//get the setup data by SOQL
List<Account> updatedAccounts = [SELECT Id, isCheckDate__c FROM Account WHERE isCheck__c =: true];
system.assertEquals(
5,
updatedAccounts.size(),
'Test_JobApplicationCommon_002: size of the retrieved accounts would be 5'
);
for(Account acc : updatedAccounts){
acc.isCheck__c = true;
}
// update test data
update updatedAccounts;
for(Account acc : updatedAccounts){
system.assertEquals(
Date.today(),
acc.isCheckDate__c,
'Test_JobApplicationCommon_002: The isCheckDate__c should be updated as todate'
);
}
}
// test method 2 which usues defined test data
@isTest static void Test_TestAccount_002() {
//get the setup data by SOQL
List<Account> updatedAccounts = [SELECT Id, isCheckDate__c FROM Account WHERE isCheck__c =: true];
//new test method has initialized test data.
system.assertEquals(
5,
updatedAccounts.size(),
'Test_JobApplicationCommon_002: still the size of the retrieved accounts would be 5'
);
}
}
No comments:
Post a Comment