Activity Forums Salesforce® Discussions How do you write a unit test for a salesforce trigger whose only function is to make a callout?

  • Saurabh

    Member
    May 2, 2017 at 2:53 pm

    Hi Manpreet
    Both future methods and callouts can be unit tested.

    To test future methods simply make your call to any future method between Test.startTest(); and Test.stopTest(); statements and the future method will return when Test.stopTest(); is called. See the documentation for the Test class here: System.Test

    Testing callouts is a bit trickier though. Basically 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. There's one example of this method here: http://www.iterativelogic.com/unit-test-callouts-in-apex-code-part-2/

    There's also an older example of callout unit testing that uses a static variable you set in your unit test. Just replace that static variable with a call to Test.isRunningTest() and their example works fairly well as well. That example can be found here: http://sfdc.arrowpointe.com/2009/05/01/testing-http-callouts/

    Hoping it may help you

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos