1

I have a Batchable class which does a callout.

When I test the class which like this:

Test.startTest()
MyBatchableJob job = new MyBatchableJob();
Database.executeBatch(job, 50);
Test.stopTest();

I get this error:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

But when I test like this:

Test.startTest();
MyBatchableJob job = new MyBatchableJob();
job.execute(null, [SELECT ... FROM ... ]);
Test.stopTest();

It works fine.

Does creating and executing a Batchable object using Database.executeBatch count as pending uncommitted work? or am I missing something?

  • Had similar problem, after wasting a lot of time figured that somehow Database.executeBatch is counted as a dml. Had to move all logic from execute to another @TestVisible private function and test logic separately. – Raul Jul 17 at 4:38