0

There are 2 methods will callout and insert.I wrote 2 batches to invoke my method. I use the one invoke the other one to avoid System.CalloutException,and it's useful when i run them.

global void finish(Database.BatchableContext BC) {
        Database.executeBatch(new TrelloChangeTaskBatch());
}

But when i create MockHttpCallout class and use Test.startTest() and stopTest() in my test class to run the batches.

        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new MockTrelloHttpResponseGenerator());
        Database.executeBatch(new TrelloBatch());
        Test.stopTest();

it's still happened.

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

I tried to delete startTest() and stopTest() but it's invalid.More than that i can't use @feature. Somebody told my isRunningTest() maybe useful, but i don't know how to use it in my case because there are no example in document.

Anybody know how to solve this problem in unit test? Thanks a lot!

marked as duplicate by Eric, dphil, crmprogdev, Boris Bachovski, Mohith Shrivastava Dec 14 '14 at 18:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

This is a known bug in the mock implementation and a search would have return several results.

This one being the most relevant: Getting CalloutException when testing Batchable class with Callouts and HttpCalloutMock

Regardless, in order to cover your batch class, instantiate the batch class and call the start, execute, and finish methods directly:

global class Batch_Create_Client_Accounts implements Database.Batchable<sObject> {


    global Database.QueryLocator start(Database.BatchableContext BC) {

    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {


    }

    global void finish(Database.BatchableContext BC) {

    }



}

Test:

Batch_Create_Client_Accounts b = New Batch_Create_Client_Accounts();
b.start(null);
b.execute(null,YOUR LIST OF RECORDS);
b.finish;

OR

Batch_Create_Client_Accounts b = New Batch_Create_Client_Accounts();

b.execute(null,b.start(null).getQuery());
b.finish;

Not the answer you're looking for? Browse other questions tagged or ask your own question.