Basically, I'm getting the dreaded "System.CalloutException: You have uncommitted work pending." exception even though I'm 99.999% sure I'm not making any DML statements after running Test.startTest().

I've reduced my test to the simplest form and am still getting the error.

 @isTest static void test_WTFSALESFORCE() {

    abc__c abc = new abc__c(
            Name = 'test'); 
    insert abc;

    //set mock
    MockHttpResponseGenerator mock = 
        new MockHttpResponseGenerator('http://google.com','Hello World');


    Test.setMock(HttpCalloutMock.class, mock);

    Http h = new Http();
    HttpRequest req = new HttpRequest();

    // Send the request, and return a response
    HttpResponse res = h.send(req);
    String resp = res.getBody();


It is embarrassing how long I've spent trying to debug this with no progress...

The object in question is a dummy object and not part of any workflows or triggers.

Am I missing something?

Also the test passes fine in a Full Copy Sandbox that was refreshed less than a month ago.

  • yes the issue may come from a trigger. First at least check triggers on account insert – highfive Dec 10 '15 at 5:48
  • @highfive There are tons of triggers and workflows. The log for this simple piece of code is almost 6000 lines and that's with just at debug level. I'm switching the DML to use a dummy object with no triggers or workflows to see if I still error out. – NSjonas Dec 10 '15 at 5:53
  • @highfive I tried the same test with a dummy blank object that is not being used anywhere. Same result – NSjonas Dec 10 '15 at 6:01
  • Try changing class version to 35. This code must work fine until there is no dml in between. – Ashwani Dec 10 '15 at 6:01
  • @Ashwani Holy Crap... I can't believe I didn't think of trying that :facepalm. It was already running at v33 so I'm surprised made a difference. Thanks you so much. Submit an answer just incase someone else runs across this wierdness? – NSjonas Dec 10 '15 at 6:09

Change API Version to 35. Version 35 doesn't have this issue.

  • 1
    seriously? what's the relation with that error and version number? – highfive Dec 10 '15 at 6:20
  • 1
    Changing the version solve many wierd behaviors. There can be chances that it has solved other related issue and that resolved the main issue. @highfive – Ashwani Dec 10 '15 at 6:23
  • Sooo... it seems to get my simplest use case to pass but once I added back the real world test it's failing again... I give up. No test is worth this amount of effort – NSjonas Dec 10 '15 at 6:34
  • @NSjone You will have to look end to end to see where the DML is happening. The code you have posted should run without any exceptions. – Ashwani Dec 10 '15 at 6:36
  • Else you can use a workaround. Use Test.isRunningTest and skip http.send(req) in main code. – Ashwani Dec 10 '15 at 6:37

Turns out another reason this might be happening is if you have a workflow that with an Email Alert action. You can easily see if this is happening by searching your debug log for an event of: WF_EMAIL_ALERT.

I had a project where I couldn't seem to test ANY callout, no matter how simple the setup. Turns out the Account had a workflow on it that sent an email alert if a field was blank. Of course my TestUtil method that was instantiating accounts wasn't setting this field.

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.