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
Sohan ShirodkarSohan 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.

 
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.