Testing a combination of webservice callouts and inserts

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


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!

Source : Link , Question Author : Mattisdada , Answer Author : BritishBoyinDC

Leave a Comment