You need to sign in to do that
Don't have an account?
hugo.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
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
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