0

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.

  • 1
    Without seeing your code, there's not much we can do to help you find out what you can do. At minimum, we would like to see a sscce so that we can further assist you. I assure you, there's almost certainly a way to success, but without a detailed situation, we'd be forced to fill in a lot of the blanks. Please edit your question with some code so we can better assist you. – sfdcfox Oct 8 '18 at 22:15
  • The sample code in Testing Apex Callouts using HttpCalloutMock has an example on "Testing multiple HTTP callouts". Mixing in intermediate DML operations is a separate consideration. You might need to isolate the testing of the parts involved here. Test or not, you can't have DML proceeding callouts. Except for you do the required setup DML before calling Test.startTest() – Daniel Ballinger Oct 9 '18 at 0:19
1

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);
    }
}
0

Here is a previous answer I gave which will allow you to specify which response to return either based on endpoint or content (useful if the endpoint does not change but the content of the request does. Like a filter on a work for example).

It’s the simplest way to do it. Your test method defines the response and when it is used. Does not mandate static resources and your tests have full control. The echo class can be used throughout your codebase without having to code specific responses in the echo class.

https://salesforce.stackexchange.com/a/228375/3996

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.