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. Here is 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;
            }
        
        System.assertEquals(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;
            }
        
        System.assertEquals(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 
        System.assertEquals(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;
        } catch (DmlException e) {
            //Assert Error Message
            System.assert( e.getMessage().contains('Insert failed. First exception on ' +
                'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
                'Mileage request exceeds daily limit(500): [Miles__c]'), 
                e.getMessage() );
                  
            //Assert field
            System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);
            
            //Assert Status Code
            System.assertEquals('FIELD_CUSTOM_VALIDATION_EXCEPTION' , 
                                 e.getDmlStatusCode(0) );
        } //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.

  1. Add text to the debug log, indicating the next step of the code:
    System.debug('Inserting 300 more miles...single record validation');
    
  2. 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;
    
  3. 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;
        }
    
  4. Use the system.assertEquals method to verify that the expected result is returned:
    System.assertEquals(singletotalMiles, totalMiles);
    
  5. Before moving to the next test, set the number of total miles back to 0:
    totalMiles = 0;
    
  6. 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');
  7. 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; 
    
  8. Use System.assertEquals 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;
        }
           System.assertEquals(maxtotalMiles, totalMiles);
    

Negative Test Case

The following steps through the above code, in particular, the negative test case.

  1. Create a static test method called runNegativeTestCases:
    static testMethod void runNegativeTestCases(){
    
  2. Add text to the debug log, indicating the next step of the code:
    System.debug('Inserting 501 miles... negative test case');
    
  3. Create a Mileage__c record with 501 miles.
    Mileage__c testMiles3 = new Mileage__c(Miles__c = 501, Date__c = System.today());
  4. Place the insert statement within a try/catch block. This allows you to catch the validation exception and assert the generated error message.
    try {
         insert testMiles3;
         } catch (DmlException e) { 
    
    
  5. Now use the System.assert and System.assertEquals to do the testing. Add the following code to the catch block you previously created:
    //Assert Error Message
       System.assert(e.getMessage().contains('Insert failed. First exception '+
          'on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, '+
          'Mileage request exceeds daily limit(500): [Miles__c]'), 
             e.getMessage()); 
    
    //Assert Field
       System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);
    
    //Assert Status Code
       System.assertEquals('FIELD_CUSTOM_VALIDATION_EXCEPTION'  , 
                            e.getDmlStatusCode(0));
              }
       }
    }

Testing as a Second User

The following steps through the above code, in particular, running as a second user.

  1. Before moving to the next test, set the number of total miles back to 0:
    totalMiles = 0;
    
  2. Set up the next user.
           User u2 = [SELECT Id FROM User WHERE Alias='tuser'];
           System.RunAs(u2){
    
  3. 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');
    
  4. Then insert one Mileage__c record:
    Mileage__c testMiles3 = new Mileage__c(Miles__c = 100, Date__c = System.today());
    insert testMiles3;
    
  5. 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;
        }
    
  6. Use the system.assertEquals method to verify that the expected result is returned:
    System.assertEquals(u2Miles, totalMiles);
    
Previous
Next