Skip to main content
Post Closed as "Duplicate" by Raul, Vijay Ganji, Pranay Jaiswal, battery.cord, glls
edited body
Source Link
Robs
  • 9.4k
  • 21
  • 119
  • 224

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();
Test.startTest();
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?

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:

MyBatchableJob job = new MyBatchableJob();
Test.startTest();
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?

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?

Source Link
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:

MyBatchableJob job = new MyBatchableJob();
Test.startTest();
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?