The common system-defined unit test methods are:

  1. startTest: startTest method marks the point in your test code when the test actually begins. 
  2. stopTest: stopTest method comes after startTest method and marks the end point of an actual test code.

Purpose:

Any code that executes after the call to start test() and before stop test () is assigned a new set of governor limits.

Any code that executes after the call to stop test() is arranged the original limits that were in effect before start test() was called.

Ex:

@isTest

private class myClass {

     static testMethod void myTest() {

      // Create test data

        ………

        ………        

        Test.startTest();

         // Actual apex code testing

         ………

        Test.stopTest();

    }

}

isRunningTest:

This method returns true if the currently executing code was called by code contained in a test method, otherwise false.

Ex:

public class PositionTriggerHandler{

public static void processUpdatedRecords(List<Position__c> updateList) {

if (Test.isRunningTest()) {  

    //unit testing alternative code goes here

}

else {  

    //execute normally

}

}

}

setFixedSearchResults:

This method defines a list of fixed search result IDs which are then returned as results in SOSL statements in a test method.

Ex:

@isTest

private class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {

       Id [] fixedSearchResults= new Id[1];

       fixedSearchResults[0] = ‘aOOx0000003G89h’;

       Test.setFixedSearchResults(fixedSearchResults);

       List<List<SObject>> searchList = [FIND ‘Salesforce’ 

                      IN ALL FIELDS RETURNING 

                      Position__c(id, name WHERE name = ‘Salesforce’ LIMIT 1)];

    }

}

setCurrentPage:

setCurrentPage and setCurrentPageReference are Visualforce test methods that set the current PageReference for the Visualforce controllers.

setCurrentPageReference:

We will learn about these methods in the coming chapters.

Our Recent Blog

Share This Post