Sub topic 3
Routing Several Apex Callouts in One Test
This chapter assumes basic Apex tests, HttpCalloutMock, HTTP status codes, and JSON. A routing mock lets one test return different responses for different requests.
Routing several callouts in one test
A routing mock uses the request method and endpoint as a key. It can also record requests for later assertions.
@IsTest
private class WeatherRoutingTest {
@IsTest
static void routesRegisteredEndpoints() {
RoutingMock mock = new RoutingMock();
mock.add('GET', 'https://api.example.com/weather?city=Oslo',
200, '{"condition":"Rain"}');
mock.add('GET', 'https://api.example.com/weather?city=Paris',
200, '{"condition":"Sunny"}');
Test.setMock(HttpCalloutMock.class, mock);
Test.startTest();
String oslo = WeatherService.getCondition('Oslo');
String paris = WeatherService.getCondition('Paris');
Test.stopTest();
Assert.areEqual('Rain', oslo);
Assert.areEqual('Sunny', paris);
Assert.areEqual(2, mock.requests.size());
}
}The mock should return a deliberate error, such as 404, for an unregistered route. This exposes unexpected endpoints instead of hiding them.
Mocking with static resources
StaticResourceCalloutMock reads a response body from a static resource. It keeps large JSON fixtures out of Apex code.
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('WeatherResponse');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json');
Test.setMock(HttpCalloutMock.class, mock);Use MultiStaticResourceCalloutMock when different endpoints need different static resources. Use a custom mock when the response must depend on request data.
DML before callouts and `Test.startTest`
Create records before Test.startTest(). Then register the mock and execute the code under test inside the test boundary. This separates setup work from the callout's governor-limit context and avoids uncommitted DML before the callout.
Testing asynchronous callouts
For Queueable or future Apex, register the mock before enqueueing the work. Put the enqueue operation between Test.startTest() and Test.stopTest(). Salesforce runs the asynchronous work at stopTest().
A Queueable class that makes a callout must implement Database.AllowsCallouts.
Test.setMock(HttpCalloutMock.class,
new WeatherServiceMock(200, '{"condition":"Rain"}'));
Test.startTest();
System.enqueueJob(new WeatherSyncJob('Oslo'));
Test.stopTest();A note on WebServiceMock for SOAP
SOAP callouts use WebServiceMock, not HttpCalloutMock. Register it with Test.setMock(WebServiceMock.class, mock). The mock returns the SOAP response expected by the generated client.
Best practices and common pitfalls
- Register the mock before any callout can occur.
- Test success, server errors, malformed bodies, and missing routes.
- Assert the response and important request fields.
- Keep static JSON fixtures small and named clearly.
- Always call
Test.stopTest()when testing asynchronous work. - Do not assert only that no exception occurred.
Exercise
Why must a Queueable callout test register its mock before calling System.enqueueJob?
Show answer
The queued job uses the registered mock when it runs at Test.stopTest(); without it, the callout cannot be completed safely in the test.