fbpx

Free Salesforce Developers Tutorials >

Chapter 11 - Apex Testing >

System-Defined Methods

System-Defined Methods

What You’ll Learn


S2 Labs

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

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *