Testing HttpCallout with HttpCalloutMock and UnitTest Created Data

  • We have a callout to an external webservice that requires retrieving some data from the database via SOQL and then issuing an HTTP GET request, and performing various logic based upon the response. Trying to make our unit tests as robust as possible, we try to always create test data from within the unit tests. However, it seems that creating test data within a unit test, and then trying to do a callout, even when using HttpCalloutMock and Test.startTest() and Test.stopTest() will still throw an exception.

    While we could use some hard-coded values in the callout unit-test, we'd really prefer not to, as we'd like to have our tests cover as much of the end-to-end as possible, including retrieving the data from the database (which won't exist unless we create it from within the unit test).

    We could separate the logic out into multiple unit tests, one for testing the retrieving of the data, and another for testing the callout mechanism, but I'd worry there may be logic branches that rely on not only data in the database, but also certain HttpResponses that may be hard to traverse without muddying-up the actual non-test code.

    Here is a very much simplified example that shows the problem.

    Callout Class:

    public class CalloutClass {
        public static HttpResponse getInfoFromExternalService(Id myCaseId) {
            Case myCase = [SELECT Id, Account.Integration_Key__c FROM Case WHERE Id = :myCaseId LIMIT 1]; 
            HttpRequest req = new HttpRequest();
            req.setEndpoint('http://api.salesforce.com/foo/bar?id=' + myCase.Account.Integration_Key__c);
            Http h = new Http();
            HttpResponse res = h.send(req);
            return res;

    And the HttpCalloutMock Class:

    global class ExampleCalloutMock implements HttpCalloutMock{
      global HttpResponse respond(HTTPRequest req){
        HttpResponse res = new HttpResponse();
        res.setBody('GREAT SCOTT');
        return res;

    And the TestClass:

    private class ExampleCallout_Test{
      static Case getTestCase(){
        Case myCase = new Case(Subject = 'Test');
        insert myCase;
        return myCase;
      static testMethod void shouldBeAbleToGetData(){
        Case myCase = getTestCase();
        Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
        HttpResponse res = CalloutClass.getInfoFromExternalService(myCase.Id);

    And here is the result:

    15:30:18.239 (5239722000)|EXCEPTION_THROWN|[45]|System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

    I'm curious if perhaps I'm missing something, if this is a bug, or is it by design to not allow setting up test data when performing a mocked callout? If there are any creative solutions or workarounds I'd love to hear them as well. :)

    Cross-posting an answer I gave on a blog comment: "I've confirmed that you cannot use mock callouts with pending DML. The problem is that we would want to catch the erroneous case where non-test code was doing DML before a callout. Filing a bug to see if we can maybe allow DML and callouts where there is a Test.startTest() between them."

    @metadaddy Any follow up on this?

    @DanielBallinger Bug (well, actually a 'user story') filed. It's on the Apex team's backlog. No scheduled build yet.

    @DanielBallinger I'm pretty sure this is fixed in Spring 13. I solved this error using Test.startTest() in a CS3 Sandbox(Spring 13) before I saw this bug. I then refreshed my Sandbox and it moved to CS1 (Winter 13) whereupon my previously passing tests began to fail with this error. I then came here and found out why, can't wait until Spring 13, time to workaround it...

  • One option, although not ideal, would be to use @isTest(SeeAllData=true) rather than creating the Case in your test case.

    You can't make callouts once you have made changes to the database (in this case the Case insertion proceeds the callout). It seems like this applies to test case setup as well even though it occurs prior to the Test.startTest().

    Another option would be to make the mock callout in an @future method and invoke this future method within the Test.startTest() and Test.stopTest(). Updated - This doesn't resolve the issue (at least if the future method is defined in a test class). The CalloutException still occurs with the future method appearing at the bottom of the stacktrace.

    To save others the effort, the test data mixed mode DML solution of wrapping the setup code in System.runAs(user) { ... } doesn't help either. Thought it was worth a long shot, but it didn't help.

    For reference, the forum post Test method custom setting and callout - Uncommitted work pending covers much the same question. They ultimately reference another post by Bob Buzzard with the suggested resolutions:

    You can't make callouts, HTTP or otherwise, once you have made changes to the database. You either need to commit the transaction, make the callout prior to any database changes or move your callout to an @future method.

    Ideas: Allow loading test data prior to callout testing is worth promoting.

    Yeah, we're trying to avoid the seeAllData=true so we can run this test in a clean-org. I really like the idea of making the mock callout @future, I'll give that a whirl.

    I wonder if you can do @ future in an @ isTest context. Would be good to know.

    @techtrekker My understanding was the Test.startTest() and stopTest() are used to inline Asynchronous calls such as `@future` for testing. This is how I've always used them. Or did you mean something else, such as using the @future attribute on a method within the `@isTest` class? In which case, I don't know either!

    Yeah I meant the latter :)

    Funny what comes around. Thought I'd try testing a WebServiceCallout.invoke using WebServiceMock and ran straight into the CalloutException issue.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM