Sub topic 10
Mocking with static resources
This chapter assumes you can write a custom HttpCalloutMock and register it with Test.setMock, as covered earlier. It also assumes you know how to find Static Resources in Setup or in a Salesforce DX project.
Why use a static resource
Large JSON bodies are awkward inside Apex strings. They need escaped quotes, they are hard to read, and they count against code size. A static resource keeps the body in its own file. Salesforce provides two built-in mocks that read from it, so you do not write a mock class.
| Class | Use it when |
|---|---|
| `StaticResourceCalloutMock` | The test makes one callout, or every callout can get the same response. |
| `MultiStaticResourceCalloutMock` | Different endpoints need different response bodies. |
Create the static resource
In a DX project, a static resource is a content file plus a metadata file in force-app/main/default/staticresources. The name of the resource comes from the file name.
WeatherResponse.json:
{"condition":"Sunny","temperature":21}WeatherResponse.resource-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
<cacheControl>Private</cacheControl>
<contentType>application/json</contentType>
</StaticResource>Deploy both files before you run the test. If the resource is missing, the test fails when the mock tries to read it, not when you register it.
A single static resource mock
This is the test from the topic plan, using WeatherService.
@IsTest
private static void usesStaticResource() {
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('WeatherResponse');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json');
Test.setMock(HttpCalloutMock.class, mock);
Test.startTest();
String condition = WeatherService.getCondition('Oslo');
Test.stopTest();
Assert.areEqual('Sunny', condition, 'The body should come from the static resource');
}Set the status code yourself. The mock only returns what you configure, and WeatherService checks for 200. Use the resource name exactly as it appears in Setup, without the file extension.
Several endpoints with MultiStaticResourceCalloutMock
MultiStaticResourceCalloutMock maps a full URL to a resource name. It is useful when the code under test calls more than one endpoint.
@IsTest
private static void routesByEndpoint() {
MultiStaticResourceCalloutMock mock = new MultiStaticResourceCalloutMock();
mock.setStaticResource('https://api.example.com/weather?city=Oslo', 'WeatherResponse');
mock.setStaticResource('https://api.example.com/weather?city=Rome', 'WeatherRomeResponse');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json');
Test.setMock(HttpCalloutMock.class, mock);
Test.startTest();
String oslo = WeatherService.getCondition('Oslo');
String rome = WeatherService.getCondition('Rome');
Test.stopTest();
Assert.areNotEqual(oslo, rome, 'Each city should get its own body');
}The URL you register must match the endpoint the code builds, including the query string. This test assumes a second resource named WeatherRomeResponse exists.
Common errors
- Resource not found. The name is wrong, the resource was not deployed, or the file extension was included in the name.
- Unmatched endpoint. With the multi mock, a URL that differs from the registered one by a parameter or a trailing slash gets no response. Print
req.getEndpoint()in a scratch test to see what the code sends. - Unexpected failure on a 200 test. You forgot
setStatusCode. - Stale test data. Static resources are shared by every test. Changing the file changes all tests that use it.
Exercise
Your service calls https://api.example.com/weather?city=Oslo and https://api.example.com/alerts?city=Oslo. You want a different JSON body for each, both with status 200. Which mock class do you use, and what must each setStaticResource call receive?
Show answer
Use MultiStaticResourceCalloutMock. Each call to setStaticResource takes the full endpoint URL, including the query string, and the name of the static resource without its extension. Call setStatusCode(200) once on the mock to apply it to both responses.