Experience Salesforce

System-Defined Methods

What You’ll Learn

  • System-Defined Unit Test 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 Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale