Newer Version Available
Testing HTTP Callouts Using Static Resources
You can test HTTP callouts by specifying the body of the response you’d like to receive in a static resource and using one of two built-in classes—StaticResourceCalloutMock or MultiStaticResourceCalloutMock.
Testing HTTP Callouts Using StaticResourceCalloutMock
Apex provides the built-in StaticResourceCalloutMock class that you can use to test callouts by specifying the response body in a static resource. When using this class, you don’t have to provide your own implementation of the HttpCalloutMock interface. Instead, just create an instance of StaticResourceCalloutMock and set the static resource to use for the response body, along with other response properties, like the status code and content type.
- Create a text file that contains the response body to return. The response body can be an arbitrary string, but it must match the content type, if specified. For example, if your response has no content type specified, the file can include the arbitrary string abc. If you specify a content type of application/json for the response, the file content should be a JSON string, such as {"hah":"fooled you"}.
- Create a static resource for the text file:
- From Setup, enter Static Resources in the Quick Find box, then select Static Resources.
- Click New.
- Name your static resource.
- Choose the file to upload.
- Click Save.
To learn more about static resources, see “Defining Static Resources” in the Salesforce online help.
Next, create an instance of StaticResourceCalloutMock and set the static resource, and any other properties.
1StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
2mock.setStaticResource('myStaticResourceName');
3mock.setStatusCode(200);
4mock.setHeader('Content-Type', 'application/json');In your test method, call Test.setMock to set the mock callout mode and pass it HttpCalloutMock.class as the first argument, and the variable name that you created for StaticResourceCalloutMock as the second argument.
1Test.setMock(HttpCalloutMock.class, mock);After this point, if your test method performs a callout, the callout is not made and the Apex runtime sends the mock response you specified in your instance of StaticResourceCalloutMock.
This is a full example containing the test method (testCalloutWithStaticResources) and the method it is testing (getInfoFromExternalService) that performs the callout. Before running this example, create a static resource named mockResponse based on a text file with the content {"hah":"fooled you"}. Save each class separately and run the test in CalloutStaticClassTest.
1public class CalloutStaticClass {
2 public static HttpResponse getInfoFromExternalService(String endpoint) {
3 HttpRequest req = new HttpRequest();
4 req.setEndpoint(endpoint);
5 req.setMethod('GET');
6 Http h = new Http();
7 HttpResponse res = h.send(req);
8 return res;
9 }
10}1@isTest
2private class CalloutStaticClassTest {
3 @isTest static void testCalloutWithStaticResources() {
4 // Use StaticResourceCalloutMock built-in class to
5 // specify fake response and include response body
6 // in a static resource.
7 StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
8 mock.setStaticResource('mockResponse');
9 mock.setStatusCode(200);
10 mock.setHeader('Content-Type', 'application/json');
11
12 // Set the mock callout mode
13 Test.setMock(HttpCalloutMock.class, mock);
14
15 // Call the method that performs the callout
16 HTTPResponse res = CalloutStaticClass.getInfoFromExternalService(
17 'http://api.salesforce.com/foo/bar');
18
19 // Verify response received contains values returned by
20 // the mock response.
21 // This is the content of the static resource.
22 System.assertEquals('{"hah":"fooled you"}', res.getBody());
23 System.assertEquals(200,res.getStatusCode());
24 System.assertEquals('application/json', res.getHeader('Content-Type'));
25 }
26}Testing HTTP Callouts Using MultiStaticResourceCalloutMock
Apex provides the built-in MultiStaticResourceCalloutMock class that you can use to test callouts by specifying the response body in a static resource for each endpoint. This class is similar to StaticResourceCalloutMock except that it allows you to specify multiple response bodies. When using this class, you don’t have to provide your own implementation of the HttpCalloutMock interface. Instead, just create an instance of MultiStaticResourceCalloutMock and set the static resource to use per endpoint. You can also set other response properties like the status code and content type.
First, you must create a static resource from a text file to contain the response body. See the procedure outlined in Testing HTTP Callouts Using StaticResourceCalloutMock.
Next, create an instance of MultiStaticResourceCalloutMock and set the static resource, and any other properties.
1MultiStaticResourceCalloutMock multimock = new MultiStaticResourceCalloutMock();
2multimock.setStaticResource('http://api.salesforce.com/foo/bar', 'mockResponse');
3multimock.setStaticResource('http://api.salesforce.com/foo/sfdc', 'mockResponse2');
4multimock.setStatusCode(200);
5multimock.setHeader('Content-Type', 'application/json');In your test method, call Test.setMock to set the mock callout mode and pass it HttpCalloutMock.class as the first argument, and the variable name that you created for MultiStaticResourceCalloutMock as the second argument.
1Test.setMock(HttpCalloutMock.class, multimock);After this point, if your test method performs an HTTP callout to one of the endpoints http://api.salesforce.com/foo/bar or http://api.salesforce.com/foo/sfdc, the callout is not made and the Apex runtime sends the corresponding mock response you specified in your instance of MultiStaticResourceCalloutMock.
This is a full example containing the test method (testCalloutWithMultipleStaticResources) and the method it is testing (getInfoFromExternalService) that performs the callout. Before running this example, create a static resource named mockResponse based on a text file with the content {"hah":"fooled you"} and another named mockResponse2 based on a text file with the content {"hah":"fooled you twice"}. Save each class separately and run the test in CalloutMultiStaticClassTest.
1public class CalloutMultiStaticClass {
2 public static HttpResponse getInfoFromExternalService(String endpoint) {
3 HttpRequest req = new HttpRequest();
4 req.setEndpoint(endpoint);
5 req.setMethod('GET');
6 Http h = new Http();
7 HttpResponse res = h.send(req);
8 return res;
9 }
10}1@isTest
2private class CalloutMultiStaticClassTest {
3 @isTest static void testCalloutWithMultipleStaticResources() {
4 // Use MultiStaticResourceCalloutMock to
5 // specify fake response for a certain endpoint and
6 // include response body in a static resource.
7 MultiStaticResourceCalloutMock multimock = new MultiStaticResourceCalloutMock();
8 multimock.setStaticResource(
9 'http://api.salesforce.com/foo/bar', 'mockResponse');
10 multimock.setStaticResource(
11 'http://api.salesforce.com/foo/sfdc', 'mockResponse2');
12 multimock.setStatusCode(200);
13 multimock.setHeader('Content-Type', 'application/json');
14
15 // Set the mock callout mode
16 Test.setMock(HttpCalloutMock.class, multimock);
17
18 // Call the method for the first endpoint
19 HTTPResponse res = CalloutMultiStaticClass.getInfoFromExternalService(
20 'http://api.salesforce.com/foo/bar');
21 // Verify response received
22 System.assertEquals('{"hah":"fooled you"}', res.getBody());
23
24 // Call the method for the second endpoint
25 HTTPResponse res2 = CalloutMultiStaticClass.getInfoFromExternalService(
26 'http://api.salesforce.com/foo/sfdc');
27 // Verify response received
28 System.assertEquals('{"hah":"fooled you twice"}', res2.getBody());
29 }
30}