WebServiceMock causing “CalloutException: You have uncommitted work pending” when creating managed package

I have a number of test methods that call Test.setMock(WebServiceMock.class, new SomeWebServiceMockImpl()); where SomeWebServiceMockImpl implements WebServiceMock.

These test methods pass when I run the tests via:

  • Setup > App Setup > Develop > Apex Test Execution.
  • Setup > App Setup > Develop > Apex Classes > class name > Run Tests.
  • Eclipse > Run Tests
  • the Developer Console Tests tab

However, when I try and create a managed package that includes these tests they fail with the message:

System.CalloutException: You have uncommitted work pending. Please
commit or rollback before calling out

I understand that this message usually appears after performing DML inserts/updates – see Testing HttpCallout with HttpCalloutMock and UnitTest Created Data. As such, I’ve been careful in these test methods not to make any database calls.

Why are these tests failing during the managed package creation but otherwise passing?

I remember reading about this recently but can’t recall where. I suspect it is a known issue. I thought the work around was to execute the Mock tests first, so I moved them all to the top of the class and prefixed them with ‘aaa_’. This didn’t resolve the issue.


From the same thread as Daniel Ballinger’s answer, BBeaird points out that wrapping the mock callouts in Test.startTest() and Test.stopTest() also works around the bug.

Source : Link , Question Author : Daniel Ballinger , Answer Author : xn.

Leave a Comment