function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hugo.charbonneau1.391028108820853E12hugo.charbonneau1.391028108820853E12 

Unit testing with callout conflicts with workflow rule

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
ShaTShaT
Hi,

This error comes when the object you are using in callOut is not created.
May be some validation rule has fired when you are creating account object from test class.

Put a debug log and check if account is created or not.

static testMethod void Test_TestWS() {
     Account customer = new Account( Name = 'A' );
     INSERT customer;
  
system.debug('--------Account--------'+customer);

      Test.startTest();
      Test.setMock(HttpCalloutMock.class, new UTest_Mock_HttpResponseGenerator());

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

Thanks
Shailu
Erica CoxErica Cox
I just ran into this exact issue. Did you ever figure out a way around it other than deactivating the rule for deployment? IMO this should be a known issue.