Learn from industry experts! Admin batch starts from 19 Feb.
Hurry up!
Book your slot now!

11

System-Defined Methods

System-Defined Unit Test Methods

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 the 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 the stop test () is assigned a new set of governor limits.

Any code that executes after the call to stop test() is arranged with the original limits that were in effect before the 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:

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.

Salesforce-Admin-Training-Banner
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!