I am trying to write unit test for a trigger which calls batch apex performing callouts first then updates the response to those record.
Trigger's psuedo code looks like:
trigger AccountTrigger on Account (after update) {
if(myCondition) {
Database.executeBatch(new MyBatchClass(), 1);
}
}
And batch is:
public class MyBatchClass implements Database.Batchable<SObject>,
Database.AllowsCallouts {
// only relevant execute method
public void execute(Database.BatchableContext batchableContext,
List<Account> accounts) {
for(Account account: accounts) {
// make http request callout
// based on response update account
}
}
}
When I run my test test, it gives me error as:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
From the related question, I understand that Database.executeBatch
is treated as a DML. When implementing this solution, I get:
System.CalloutException: Callout from triggers are currently not supported.
Please suggest any workaround or point towards any gap in the design. Any insight is highly appreciated.