Sub topic 5
Registering the mock with Test.setMock
This chapter assumes you can write Apex classes and @IsTest methods, and that you know the HttpCalloutMock interface from the previous chapter. Here you connect a mock to the code under test. The step is small, but it is where most first-time callout tests fail.
What Test.setMock does
Test.setMock tells the Apex runtime which mock to use when code in the current test makes a callout. Its signature is:
Test.setMock(Type interfaceType, Object instance);For HTTP callouts, the first argument is HttpCalloutMock.class. The second argument is an instance of a class that implements HttpCalloutMock. After the call, any new Http().send(req) in that test runs your respond method. The request never leaves Salesforce.
Test.setMock(HttpCalloutMock.class, new WeatherServiceMock(200, '{"condition":"Sunny"}'));The code under test does not change. WeatherService still builds an HttpRequest and calls send. The runtime swaps in the mock behind the scenes.
Where to call it
Call Test.setMock before the callout runs. The usual place is the first line of the test method, before Test.startTest().
@IsTest
static void returnsConditionOnSuccess() {
Test.setMock(HttpCalloutMock.class, new WeatherServiceMock(200, '{"condition":"Sunny"}'));
Test.startTest();
String condition = WeatherService.getCondition('Oslo');
Test.stopTest();
Assert.areEqual('Sunny', condition, 'The condition should come from the mocked body');
}If you call getCondition first and register the mock afterwards, the callout has already run without a mock. The test fails with System.CalloutException: You have uncommitted work pending or, more often, Methods defined as TestMethod do not support Web service callouts.
Sequence of events
The diagram shows what happens when the test runs.
Rules to remember
- The mock is scoped to one test method. Register it again in every test that makes a callout.
- Only one mock per interface is active at a time. A second
Test.setMock(HttpCalloutMock.class, ...)call replaces the first. - The instance can hold state. Constructor arguments such as the status code and body let one class serve many tests.
- The class you pass must implement
HttpCalloutMock. Passing another type fails at runtime.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Registering the mock after the callout | `Methods defined as TestMethod do not support Web service callouts` | Move `Test.setMock` to the top of the test |
| Forgetting it in one test | Only that test fails with the same error | Add the call, or use a shared helper method |
| Passing `HttpCalloutMock` instead of `HttpCalloutMock.class` | Compile error | Use the `.class` form for the first argument |
| Using a class that does not implement the interface | Compile or runtime type error | Add `implements HttpCalloutMock` |
Exercises
Exercise
A test calls WeatherService.getCondition('Oslo') inside Test.startTest() and Test.stopTest(). It never calls Test.setMock. What happens, and what is the fix?
Show answer
The test fails with a CalloutException saying that test methods do not support web service callouts, because no mock is registered. The fix is to call Test.setMock(HttpCalloutMock.class, new WeatherServiceMock(200, '{"condition":"Sunny"}')) before the callout runs, normally as the first line of the test.
Exercise
A test calls Test.setMock twice with HttpCalloutMock.class, first with a 200 mock and then with a 500 mock, and then makes one callout. Which mock responds?
Show answer
The 500 mock. Only one mock per interface is active, and the second call replaces the first.