How do you write a unit test for a trigger whose only function is to make a callout?

What is considered a best practice for writing a unit test (to maintain code coverage) on a trigger whose sole function is making a callout (Which also means it has to use a future method)? Neither futures or callouts can be tested so there really isn’t anything to do.

What I’ve been doing is firing an add and/or update and just asserting that it added or updated correctly. Since there is also code to skip the callout if it’s a test even this doesn’t really test anything. Is this about the best we can hope for right now?


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:

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:

Source : Link , Question Author : Ryan Elkins , Answer Author : E.J. Wilburn

Leave a Comment