I could run unit test for webservice method call using mock very well, but when I call a webservice method and after that another webservice method in the same test, I get "You have uncommitted work pending. Please commit or rollback before calling out for multiple callout" exception. My requirement is, call webserice method to add contact and them another method to search that contact. Please hep me on this if anyone got solution for this.


1 Answer 1


If you are doing tests you test one item / process at a time. Once that process is validated you manually create the test data (created by the first process) in another test method and test the second process.

  1. Create a test to test the web service code and assert that the contact was created
  2. In another test, create the contact manually then callout to perform the search.

Outside of test methods you would not be able to do these two things at the same time so you should not need to test them both at the same time.

public class ws_tests{

     public static testmethod void create_contact_test{

        //Make web service callout
        //assert contact was created


     public static testmethod void search_contact_test{

        //Create Contact and insert using dml
        //Perform callout to search for contact



You will have to user mock callout to return the results you desire. An structure the test so the code you are actually testing is executed.

Just to make sure: when I say perform callout, I mean executed the code you are testing that performs the callout. it is pointless to call the mock method from this the test class above as you will not actually be testing anything. You should be executing the class your are testing which will perform the callout

  • Also use Test.startTest() and Test.StopTest(). The governor limits for the code executed between these 2 methods are reset back to 0. May 28, 2014 at 2:13
  • @Eric, you mean something like this: static Id cID testMethod MainTest() { Test.startTest() Test1(); Test2(); Test.stopTest() } testMethod Test1() { //add contact // webservice method call cID=contact.id; } testMethod Test2() { //search contact in webservice using cID }
    – Sanjivani
    May 28, 2014 at 5:16
  • I tried like above, but still getting uncommited error.
    – Sanjivani
    May 28, 2014 at 9:10
  • No. two SEPERATE methods. forget the main test class. I am updating my answer with the code example
    – Eric
    May 28, 2014 at 13:33
  • Of course, this assumes that the two methods making the callouts are designed in a such a way that they can be tested independently. Considering most people are not aware that Salesforce does not allow callouts after DML, their code is not setup that way. At least, they are not aware until bit by this issue. May 28, 2014 at 15:24

You must log in to answer this question.

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