Sub topic 4
Implementing HttpCalloutMock
This chapter assumes you can write Apex classes and @IsTest methods, and that you know HTTP methods, status codes, and JSON. It also assumes you have seen the Http, HttpRequest, and HttpResponse classes. Here you will build the mock that replaces the real HTTP call in a test.
What the interface asks for
HttpCalloutMock is a built-in Apex interface with one method:
HttpResponse respond(HttpRequest req);Apex calls respond instead of sending the request over the network. The HttpRequest it passes in is the one your code built. The HttpResponse you return is the one your code receives from Http.send(). Your production code does not change.
A reusable mock
The class below returns whatever status code and body the test gives it. That lets one class cover success and failure cases.
@IsTest
public class WeatherServiceMock implements HttpCalloutMock {
private Integer statusCode;
private String body;
public WeatherServiceMock(Integer statusCode, String body) {
this.statusCode = statusCode;
this.body = body;
}
public HttpResponse respond(HttpRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatusCode(statusCode);
res.setBody(body);
return res;
}
}Three details matter here:
- The
@IsTestannotation keeps the class out of your org's code size limit. It also means production code cannot call it. - The constructor stores the values, so each test decides what the "server" says.
- The method must be
publicorglobal, because it implements an interface method.
How the pieces connect
During a test, the platform sends the callout to your mock instead of the network.
The next chapter covers Test.setMock in detail. For now, remember that the mock must be registered before the code under test calls send().
Building realistic responses
Your mock only needs to set what your code reads. WeatherService reads the status code and the body, so those two are required. If your code reads a header, such as a Location or rate-limit header, set it with res.setHeader(). A missing header returns null, and a null that production code would never see can cause a misleading test failure.
Keep the body as close to a real response as you can. A body like '{}' may pass a test that a real payload would break. Copy a real response from the API documentation and trim it to the fields you use.
Common errors
| Symptom | Likely cause |
|---|---|
| `Methods defined as TestMethod do not support Web service callouts` | No mock is registered before `send()` runs. |
| `Class WeatherServiceMock must implement the method: HttpResponse respond(HttpRequest)` | The method signature differs, such as a wrong return type or a missing `public`. |
| Test passes but asserts the wrong value | The mock returned a body your code parsed differently than the real API would. |
| `System.TypeException` while parsing | The mock body is not valid JSON, often from unescaped quotes in a string literal. |
Where to put the mock
Two layouts work. A top-level @IsTest class, as above, can be shared by many test classes. An inner class inside one test class keeps the mock next to its only user. Use a shared class when several tests need the same behavior, and an inner class for a one-off response.
Exercise
Your mock returns a 200 status and a valid JSON body, but your service reads a header named X-Request-Id. The test fails with a NullPointerException. What is the likely cause and the fix?
Show answer
The mock never sets the X-Request-Id header, so res.getHeader('X-Request-Id') returns null. Add res.setHeader('X-Request-Id', 'test-123'); in the mock's respond method, or make the production code handle a missing header.