Play Games

Search This Blog

Thursday, October 28, 2021

How to skip a particular piece of code while test class is running in apex - Salesforce Globe For You

How to skip a particular piece of code while test class is running in apex -  Salesforce Globe For You

Solution: Use Test.isRunningTest() This method returns true if this code is called from test method of test class.

So in case if you need to skip piece of code when test class is running,place that code inside if(!Test.isRunningTest()){}.

Example:

public class SkipCodeController {

    public static void run() {

        if(!Test.isRunningTest()) {

            system.debug('This method runs in normal method call from normal class');

        } else {

            system.debug('This method runs only when called from Test Class');

        }

    }

}

Output: When you call this method like SkipCodeController.run() output will be



No comments:

Post a Comment