Skip to main content
2 of 2
edited body
Robs
  • 9.4k
  • 21
  • 119
  • 224

Testing Batchable with Callout Error: You have uncommitted work pending. Please commit or rollback before calling out

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?

Robs
  • 9.4k
  • 21
  • 119
  • 224