Our instance has some triggers that fire before insert and before update on accounts, contacts, and leads.

In a test class, if I am creating test records will the insertion of new account records cause a DML to run and therefore error when trying to make a subsequent callout (You have uncommitted work pending) even if I am using @TestSetup to create those records?

The batch class calls another class which actually performs the callout (just an http request that returns a string)

   Global Class FocusDomainBatch implements Database.Batchable<sObject>, Database.allowsCallouts
    global Database.querylocator start(Database.BatchableContext BC)
        String query = 'Select id,name, First_user_sign_up_date__c, Total_Users__c, New_Users_In_Last_Month__c, Avg_Fvrt_Pro_Per_User__c, Users_with_zero_fvrts__c, Users_with_2_or_less_fvrts__c, Average_daily_queries__c, Average_monthly_queries__c, Total_current_month_queries__c, Total_Queries__c from Focus_Domain_Stats__c';
        return Database.getQueryLocator(query);    

    global void execute(Database.BatchableContext BC, List<sObject> scope)
           List <Focus_Domain_Stats__c> allDomains = scope;          
           List <string> web = new List <string>();

           for(Focus_Domain_Stats__c c : allDomains)
               system.debug('FocusDomainBatch:execute for ' + c.Name);

           FocusAPI fi = new FocusAPI();//actually performs the callout
           String Token = fi.getToken();//failing here
           String stm = String.valueof(Date.today().toStartOfMonth())+' 00:00:00.000Z';
           stm = stm.replace(' ', 'T');
           List <String> dm = new List<String>();

Test Class:


Public Class TestFocusDomainBatch2
    @testSetup static void setupTestData(){

        //Setup Data 
        Lead ld = new Lead();
        ld.lastName = 'kitagawa';
        ld.email = '[email protected]';
        ld.company = 'test';
        ld.website = 'test.com';
        ld.focus_user__c = TRUE;
        ld.Focus_Sign_Up_Date__c = date.today();
        insert ld; 

        Focus_Domain_Stats__c ds = new Focus_Domain_Stats__c(); 
        ds.name = 'test.com';
        insert ds; 

        Lead ld2 = new Lead();
        ld2.lastName = 'kitagawa';
        ld2.email = '[email protected]';
        ld2.company = 'test';
        ld2.website = 'test.com';
        ld2.focus_user__c = TRUE;
        ld2.Focus_Sign_Up_Date__c = date.today();
        insert ld2; 

        Account acc = new Account(); 
        acc.name = 'test'; 
        acc.website = 'test.com';
        acc.Focus_Domain_Stats__c = ds.id;
        insert acc;

        Contact ctc = new Contact();
        ctc.lastName = 'kitagawa';
        ctc.email = '[email protected]';
        ctc.account = acc;
        ctc.accountid = acc.id;
        ctc.Focus_Sign_Up_Date__c = date.today();
        insert ctc; 

        Contact ctc2 = new Contact();
        ctc2.lastName = 'kitagawa2';
        ctc2.email = '[email protected]';
        ctc2.account = acc;
        ctc2.accountid = acc.id;
        ctc2.Focus_Sign_Up_Date__c = date.today();
        insert ctc2;

        Focus_Product__c fp = new Focus_Product__c(); 
        fp.Contact__c = ctc.id; 
        fp.Lead__c = ld.id; 
        fp.name = 'test product';
        insert fp;

    Static testMethod void TestFocusDomainBatch ()

            //set mock      
            Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator1());

            Database.executeBatch(new FocusDomainBatch());

  • 1
    You should probably close your old question and edit this one to add your new code.
    – Adrian Larson
    May 19, 2016 at 23:42
  • 1
    DML is DML, regardless of where it executes. So if DML casues a trigger to run and that does a callout, you will have an issue. Perhaps a master kill switch for the callouts that you enable in @Testsetup and then reset at beginning of testmethod
    – cropredy
    May 20, 2016 at 0:55
  • @cropredy Its more that I have a trigger that is updating records before insert so when I am setting up the test data those are being fired. If those fire before my callout, I get the unfinished work error
    – Justin
    May 20, 2016 at 13:34
  • Justin , see my blog cropredysfdc.com/2014/06/18/…
    – cropredy
    May 20, 2016 at 14:00

1 Answer 1


This is a bit tricky, need to test every method in batch job separately. Found this blog may help: how to test batch class


Not the answer you're looking for? Browse other questions tagged .