Activity Forums Salesforce® Discussions When to use Test.isRunningTest() in Salesforce Apex Class?

  • Himanshu

    Member
    August 2, 2016 at 1:23 pm

    Hi Pranav,

    isRunningTest()

    Returns true if the currently executing code was called by code contained in a test method, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.

    Other scenarios:
    1) To ensure the trigger doesn't execute the batch if Test.IsRunningTest() is true, and then test the batch class with it's own test method.
    2) Testing callouts - in your callout code you check to see if you're executing within a unit test context by checking Test.isRunningTest() and instead of getting your callout response from an HttpResponse.send() request, you return a pre-built test string instead.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos