Activity › Forums › Salesforce® Discussions › Getting Uncommitted work pending Error in my Test class
Tagged: CalloutException, DML Operation, Mock class, Salesforce Error, System CalloutException, Test Class, Web Service
-
Getting Uncommitted work pending Error in my Test class
Posted by Akash on April 13, 2016 at 11:32 AMI am using Mock class in my test class and it is throwing this error:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling outshariq replied 7 years, 7 months ago 5 Members · 5 Replies -
5 Replies
-
You might be doing DML operations before web service callout so for resolving this issue you may call DML operation and web service callout in different test methods.
- [adinserter block='9']
-
Hi Akash,
Sometimes a developer needs to create a record and then update it with information provided by a Web Service. However, you can’t do a web service callout after a DML operation in the same transaction. You should move your batch execution to the inside of the Test.startTest block.
@isTest
private class MyFunTest {
@isTest
static void runTest() {
Account a = new Account(name=’foo’);
insert a;Test.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
MyCalloutClass.getInfoFromExternalService();
Test.stopTest();
}
} -
You may try the following for System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
@isTest
private class MyFunTest {
@isTest
static void runTest() {
Account a = new Account(name=’foo’);
insert a;Test.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
MyCalloutClass.getInfoFromExternalService();
Test.stopTest();
}
} -
Hi,
Just call DML and webservice class in different transaction.
Hope this helps.
Log In to reply.