Salesforce Developers Blog

Testing HTTP Callouts with Static Data in Winter ’13

Avatar for Pat PattersonPat Patterson
One of the most eagerly awaited Winter ’13 features, at least for developers, has been the ability to test Apex callouts. In this blog entry, I look at how you can mock up callout responses using data in static resources.
Testing HTTP Callouts with Static Data in Winter ’13
October 15, 2012
Listen to this article
0:00 / 0:00

One of the most eagerly awaited Winter ’13 features, at least for developers, has been the ability to test Apex callouts. You can now test HTTP callouts by either

In this blog entry, I’ll focus on the static resource case. As an example, let’s assume that I’m retrieving JSON Account data from an external service. Here’s a simple callout that retrieves JSON account data, parses it, and returns the resulting list of accounts:

1public class CalloutAccounts {
2    public static List<Account> getAccounts() {
3        HttpRequest req = new HttpRequest();
4        req.setEndpoint('http://api.example.com/accounts');
5        req.setMethod('GET');
6        Http h = new Http();
7        HttpResponse res = h.send(req);
8        String jsonData = res.getBody();
9        List<Account> accounts = 
10            (List<Account>)JSON.deserialize(jsonData, List<Account>.class);
11        return accounts;
12    }
13}

To test this callout, first, I’ll need to create a file, accounts.json, on my local machine containing test data:

1[
2  {
3    "Name": "sForceTest1",
4    "Website": "http://www.sforcetest1.com"
5  },
6  {
7    "Name": "sForceTest2",
8    "Website": "http://www.sforcetest2.com"
9  },
10  {
11    "Name": "sForceTest3",
12    "Website": "http://www.sforcetest3.com"
13  }
14]

Now I go to Setup | Develop | Static Resources, click New Static Resource, enter the name jsonAccounts, and select accounts.json for upload. Notice that Force.com set the MIME type based on the extension of the file I uploaded:

Now to define my test method. I’ll use StaticResourceCalloutMock to provide my Account test data when the getAccounts method executes its callout. First, I need to create an instance of StaticResourceCalloutMock and set the static resource, status code and content type header:

1StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); 
2mock.setStaticResource('jsonAccounts');
3mock.setStatusCode(200);
4mock.setHeader('Content-Type', 'application/json');

Now I use Test.setMock to tell the Apex runtime I want to use this instance:

1Test.setMock(HttpCalloutMock.class, mock);

Then I can test getAccounts:

1Test.startTest();
2List<Account> accounts = CalloutAccounts.getAccounts();
3Test.stopTest();

And verify I received the expected data:

1System.assertEquals(3, accounts.size()); 
2System.assertEquals('sForceTest1', accounts[0].Name);
3System.assertEquals('sForceTest2', accounts[1].Name);
4System.assertEquals('sForceTest3', accounts[2].Name);

So, putting it all together into a test method in a test class:

1@isTest
2private class CalloutAccountsTest {
3    @isTest static void testCallout() {
4        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
5        mock.setStaticResource('jsonAccounts');
6        mock.setStatusCode(200);
7        mock.setHeader('Content-Type', 'application/json');
8
9        // Set the mock callout mode
10        Test.setMock(HttpCalloutMock.class, mock);
11
12        // Call the method that performs the callout
13        Test.startTest();
14        List accounts = CalloutAccounts.getAccounts();
15        Test.stopTest();
16
17        // Verify response received contains values returned by
18        // the mock response.
19        System.assertEquals(3, accounts.size());
20        System.assertEquals('sForceTest1', accounts[0].Name);
21        System.assertEquals('sForceTest2', accounts[1].Name);
22        System.assertEquals('sForceTest3', accounts[2].Name);
23    }
24}

In more elaborate scenarios, I might execute several callouts in a single method. For example, let’s assume I have created a file of JSON-formatted Contact data and uploaded it as the static resource jsonContacts. The method I want to test will retrieve Account data as before, then retrieve the Contacts, and go on to do some processing of the combined Account and Contact data:

1public class ProcessAccountsContacts {
2    public static String getJSON(String url) {
3        HttpRequest req = new HttpRequest();
4        req.setEndpoint(url);
5        req.setMethod('GET');
6        Http h = new Http();
7        HttpResponse res = h.send(req);
8        return res.getBody();
9    }
10
11    public static Integer processAccountsContacts() {
12        String jsonData = getJSON('http://api.example.com/accounts');
13        List<Account> accounts = 
14            (List<Account>)JSON.deserialize(jsonData, List<Account>.class);
15
16        jsonData = getJSON('http://api.example.com/contacts');
17        List<Contact> contacts = 
18            (List<Contact>)JSON.deserialize(jsonData, List<Contact>.class);
19
20        // 'Processing'
21        Integer result = accounts.size() + contacts.size();
22        return result;
23    }
24}

I’ll create an instance of MultiStaticResourceCalloutMock, configuring it to provide the appropriate static resource data depending on the URL in the callout request. Here’s the test method in its entirety:

1@isTest
2private class ProcessAccountsContactsTest {
3    @isTest static void testCallout() {
4        MultiStaticResourceCalloutMock multimock = 
5            new MultiStaticResourceCalloutMock();
6        // 3 test accounts
7        multimock.setStaticResource('http://api.example.com/accounts', 
8            'jsonAccounts');
9        // 3 test contacts
10        multimock.setStaticResource('http://api.example.com/contacts', 
11            'jsonContacts');
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 that performs the callouts
19        Test.startTest();
20        Integer result = ProcessAccountsContacts.processAccountsContacts();
21        Test.stopTest();
22
23        // Verify response is as expected, given the test data
24        System.assertEquals(6, result);
25    }
26}

So, testing callouts with static data is very straightforward, but what about more complex use cases? I’ll cover the HttpCalloutMock and WebServiceMock interfaces next time out and show you how to create dynamic response data with dependencies on query parameters or the request body.

More Blog Posts

The Developer’s Guide to Dreamforce 2025

The Developer’s Guide to Dreamforce 2025

Get ready for Dreamforce 2025, whether you’re joining in San Francisco or on Salesforce+.September 17, 2025

The Salesforce Developer’s Guide to the Winter ’26 Release

The Salesforce Developer’s Guide to the Winter ’26 Release

Learn about highlights for developers in the Winter '26 release across Lightning Web Components, Apex, Salesforce Platform developer tools, APIs, and more.September 08, 2025