[SalesForce] Multiple Http Mock Callouts in a single test class

Hi so I'm trying to make a test class for my http callouts class. Now the problem is that I understand that I cannot make multiple DML and HTTP callouts in a single transaction but the third party API that I'm using is paged. Now my logic depends on making multiple callouts in a single transaction and I also cannot use that suggested list approach and delay the DML operaitons.
So I have to enter data in two custom objects and adding the data in the second custom object is dependent on the DML operations obtained from the first HTTP callout. What do I do. It is really frustrating how things are so restricted with Salesforce.

Best Answer

You can have more than one callout in a test class. Check the Salesforce developer blogs for examples. This one looks like what you need.

https://developer.salesforce.com/blogs/developer-relations/2012/10/testing-http-callouts-with-static-data-in-winter-13.html

Copying code here for your convenience

@isTest
private class ProcessAccountsContactsTest {
@isTest static void testCallout() {
    MultiStaticResourceCalloutMock multimock = new MultiStaticResourceCalloutMock();
    // 3 test accounts
    multimock.setStaticResource('http://api.example.com/accounts','jsonAccounts');
    // 3 test contacts
    multimock.setStaticResource('http://api.example.com/contacts', 
        'jsonContacts');
    multimock.setStatusCode(200);
    multimock.setHeader('Content-Type', 'application/json');

    // Set the mock callout mode
    Test.setMock(HttpCalloutMock.class, multimock);

    // Call the method that performs the callouts
    Test.startTest();
    Integer result = ProcessAccountsContacts.processAccountsContacts();
    Test.stopTest();

    // Verify response is as expected, given the test data
    System.assertEquals(6, result);
    }
}
Related Topic