• hugo.charbonneau1.391028108820853E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hi,

I am having a problem that I investigated down to this very simple minimalistic example that can be reproduced easily.

1) Create a workflow rule on the Account object that will send an email to the sales team when an account is created

2) Create a Unit Test like this:

static testMethod void Test_TestWS() {
     Account customer = new Account( Name = 'A' );
     INSERT customer;
 
      Test.startTest();
      Test.setMock(HttpCalloutMock.class, new UTest_Mock_HttpResponseGenerator());

       MyClass.CallWebService(customer.Id);
      Test.stopTest();
}

When executing the unit test I get this:
   System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

What is the best practice to avoid this problem. I am following all guidelines in order to Mock my web service and to make sure that startTest is called AFTER my test data is inserted.

It looks like the workflow rule has some pending update in the DB that is not comitted on the startTest which causes the problem.

If I Deactivate the workflow rule, my test runs fine.

Thanks

Hugo