Testing Example
The test is used with a simple mileage tracking application. The existing code for the application verifies that not more than 500 miles are entered in a single day. The primary object is a custom object named Mileage__c. The test creates one record with 300 miles and verifies there are only 300 miles recorded. Then a loop creates 200 records with one mile each. Finally, it verifies there are 500 miles recorded in total (the original 300 plus the new ones). Here’s the entire test class. The following sections step through specific portions of the code.
@isTest
private class MileageTrackerTestSuite {
static testMethod void runPositiveTestCases() {
Double totalMiles = 0;
final Double maxtotalMiles = 500;
final Double singletotalMiles = 300;
final Double u2Miles = 100;
//Set up user
User u1 = [SELECT Id FROM User WHERE Alias='auser'];
//Run As U1
System.RunAs(u1){
System.debug('Inserting 300 miles... (single record validation)');
Mileage__c testMiles1 = new Mileage__c(Miles__c = 300, Date__c = System.today());
insert testMiles1;
//Validate single insert
for(Mileage__c m:[SELECT miles__c FROM Mileage__c
WHERE CreatedDate = TODAY
and CreatedById = :u1.id
and miles__c != null]) {
totalMiles += m.miles__c;
}
Assert.areEqual(singletotalMiles, totalMiles);
//Bulk validation
totalMiles = 0;
System.debug('Inserting 200 mileage records... (bulk validation)');
List<Mileage__c> testMiles2 = new List<Mileage__c>();
for(integer i=0; i<200; i++) {
testMiles2.add( new Mileage__c(Miles__c = 1, Date__c = System.today()) );
}
insert testMiles2;
for(Mileage__c m:[SELECT miles__c FROM Mileage__c
WHERE CreatedDate = TODAY
and CreatedById = :u1.Id
and miles__c != null]) {
totalMiles += m.miles__c;
}
Assert.areEqual(maxtotalMiles, totalMiles);
}//end RunAs(u1)
//Validate additional user:
totalMiles = 0;
//Setup RunAs
User u2 = [SELECT Id FROM User WHERE Alias='tuser'];
System.RunAs(u2){
Mileage__c testMiles3 = new Mileage__c(Miles__c = 100, Date__c = System.today());
insert testMiles3;
for(Mileage__c m:[SELECT miles__c FROM Mileage__c
WHERE CreatedDate = TODAY
and CreatedById = :u2.Id
and miles__c != null]) {
totalMiles += m.miles__c;
}
//Validate
Assert.areEqual(u2Miles, totalMiles);
} //System.RunAs(u2)
} // runPositiveTestCases()
static testMethod void runNegativeTestCases() {
User u3 = [SELECT Id FROM User WHERE Alias='tuser'];
System.RunAs(u3) {
System.debug('Inserting a record with 501 miles... (negative test case)');
Mileage__c testMiles3 = new Mileage__c( Miles__c = 501, Date__c = System.today() );
try {
insert testMiles3;
Assert.fail('DmlException expected');
} catch (DmlException e) {
//Assert Status Code
Assert.areEqual('FIELD_CUSTOM_VALIDATION_EXCEPTION', e.getDmlStatusCode(0));
//Assert field
Assert.areEqual(Mileage__c.Miles__c, e.getDmlFields(0)[0]);
//Assert Error Message
Assert.isTrue(e.getMessage().contains(
'Mileage request exceeds daily limit(500): [Miles__c]'),
'DMLException did not contain expected validation message:' + e.getMessage() );
} //catch
} //RunAs(u3)
} // runNegativeTestCases()
} // class MileageTrackerTestSuite
Positive Test Case
The following steps through the above code, in particular, the positive test case for single and multiple records.
- Add text to the debug log, indicating the next step of the code:
System.debug('Inserting 300 more miles...single record validation');
- Create a Mileage__c object and insert it into the database.
Mileage__c testMiles1 = new Mileage__c(Miles__c = 300, Date__c = System.today() ); insert testMiles1;
- Validate the code by returning the inserted records:
for(Mileage__c m:[SELECT miles__c FROM Mileage__c WHERE CreatedDate = TODAY and CreatedById = :createdbyId and miles__c != null]) { totalMiles += m.miles__c; }
- Use the Assert.areEqual method to verify that the
expected result is
returned:
Assert.areEqual(singletotalMiles, totalMiles);
- Before moving to the next test, set the number of total miles
back to 0:
totalMiles = 0;
- Validate the code by creating a bulk insert of 200 records.First, add text to the debug log, indicating the next step of the code:
System.debug('Inserting 200 Mileage records...bulk validation');
- Then insert 200 Mileage__c records:
List<Mileage__c> testMiles2 = new List<Mileage__c>(); for(Integer i=0; i<200; i++){ testMiles2.add( new Mileage__c(Miles__c = 1, Date__c = System.today()) ); } insert testMiles2;
- Use Assert.areEqual to verify that the expected result
is
returned:
for(Mileage__c m:[SELECT miles__c FROM Mileage__c WHERE CreatedDate = TODAY and CreatedById = :CreatedbyId and miles__c != null]) { totalMiles += m.miles__c; } Assert.areEqual(maxtotalMiles, totalMiles);
Negative Test Case
The following steps through the above code, in particular, the negative test case.
- Create a static test method called runNegativeTestCases:
static testMethod void runNegativeTestCases(){
- Add text to the debug log, indicating the next step of the code:
System.debug('Inserting 501 miles... negative test case');
- Create a Mileage__c record with 501 miles.
Mileage__c testMiles3 = new Mileage__c(Miles__c = 501, Date__c = System.today());
- Place the insert statement within a try/catch block. This allows you to catch the validation exception and
assert the generated error message. Use the Assert.fail method to clearly assert that you expect the
validation exception.
try { insert testMiles3; Assert.fail('DmlException expected'); } catch (DmlException e) {
- Now use the Assert.areEqual and Assert.isTrue methods to do the testing.
Add the following code to the catch
block you previously
created:
//Assert Status Code Assert.areEqual('FIELD_CUSTOM_VALIDATION_EXCEPTION', e.getDmlStatusCode(0)); //Assert field Assert.areEqual(Mileage__c.Miles__c, e.getDmlFields(0)[0]); //Assert Error Message Assert.isTrue(e.getMessage().contains( 'Mileage request exceeds daily limit(500): [Miles__c]'), 'DMLException did not contain expected validation message:' + e.getMessage() );
Testing as a Second User
The following steps through the above code, in particular, running as a second user.
- Before moving to the next test, set the number of total miles
back to 0:
totalMiles = 0;
- Set up the next user.
User u2 = [SELECT Id FROM User WHERE Alias='tuser']; System.RunAs(u2){
- Add text to the debug log, indicating the next step of the code:
System.debug('Setting up testing - deleting any mileage records for ' + UserInfo.getUserName() + ' from today');
- Then insert one Mileage__c record:
Mileage__c testMiles3 = new Mileage__c(Miles__c = 100, Date__c = System.today()); insert testMiles3;
- Validate the code by returning the inserted records:
for(Mileage__c m:[SELECT miles__c FROM Mileage__c WHERE CreatedDate = TODAY and CreatedById = :u2.Id and miles__c != null]) { totalMiles += m.miles__c; }
- Use the Assert.areEqual method to verify that the
expected result is
returned:
Assert.areEqual(u2Miles, totalMiles);