I have quite the conundrum and I'm not sure how to work around it.

I have a batch method which it's entire purpose is to do a webservice callout and write to the database about what happened afterwards.

The problem I'm running into is that during a test you cannot have uncommitted database writes pending (you cannot commit during a test as far as I'm aware) and perform a webservice callout (even if it is just using mock interface).

How do you work around this issue?

NOTE: To perform the test I also have to create a bunch of fake accounts, products and other misc peices. So even if I tried to workaround it by moving the database writes somewhere else / webservice somewhere else. It would still run into this issue.

NOTE2: It gives the following error when trying:

You have uncommitted work pending. Please commit or rollback before calling out
  • 5
    Have you tried to insert all your test (fake) records and once you've done that to wrap the rest of your code into Test.StartTest() and Test.StopTest()? As you can't do DML before calling out and the limits are being reset for the code between these 2 methods, it should work. Jun 14, 2013 at 4:33
  • @e-bacho2.0 that should be an answer, not a comment ;)
    – eyescream
    Jun 14, 2013 at 6:46
  • @e-bacho2.0 Its a good answer for me as well, also note the new features in the Summer'13 release notes around testing call outs and DML. I'm looking forward to trying this out next week when my org is updated. login.salesforce.com/help/doc/en/… Jun 14, 2013 at 7:42

1 Answer 1


To start out, let me say, I feel your pain...web callouts, batches and test scripts are not a pretty combination. Here's my understanding of what is and is not possible...as the comment says, you CAN now create test records, declare a mock interface, wrap the actual code that will trigger the callout in Start/Stop test calls, and it works just fine...the uncommitted errors we used to see have been fixed. (see here for an example)

But where it seems to fall down is doing the same thing with batches. To Andrew's point about Summer 13, I too had high hopes, but my initial tests weren't promising. Prior to summer 13, I think any async request when combined with callouts in tests caused the uncommitted error. It looks to me that they have fixed the general issue - i.e. if you follow their guidelines, you can create test records, trigger a separate async request and a callout in the same test and it now works. BUT - if that async request is the actual batch calling the web callout it still seems to fail. I tried every combination of the examples with a batch calling a callout with a mock interface declared, and I couldn't get passed that error. I ended using the typical isrunningtest() check in the batch, and testing the callout separately. If someone does get all those pieces working, please let me know!

  • I ended up working around the issue by making my code worse to actually use but more compatible with the Apex Testing, you are very much correct with your post so I'm marking it as correct.
    – Mattisdada
    Jun 17, 2013 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .