You need to sign in to do that
Don't have an account?
Sohan Shirodkar
Unit test for batch apex with REST API callout
I am writing a unit test class for batch apex which makes API callout for each record in the batch.
This always fails with System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out. This exception occurs when we have a DML operation before making API callout. I have inspected debug logs, but found no such DML.
The batch class only fails in a test class, but perfectly works outside the context of a test class. Any advice would be appreciated.
Test.setMock(HttpCalloutMock.class, new TestMockHTTpResponseFactory()); Test.startTest(); CAS_TW_New batch = new CAS_TW_New(); DataBase.executeBatch(batch,5); Test.stopTest(); Integer count = [SELECT Count() FROM ObjectSF WHERE REQUESTFLAG__c = 'Already sent']; System.assert(count == 30);
This always fails with System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out. This exception occurs when we have a DML operation before making API callout. I have inspected debug logs, but found no such DML.
The batch class only fails in a test class, but perfectly works outside the context of a test class. Any advice would be appreciated.