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)
{
try
{
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);
web.add(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:
@IsTest
Public Class TestFocusDomainBatch2
{
@testSetup static void setupTestData(){
//Setup Data
Lead ld = new Lead();
ld.lastName = 'kitagawa';
ld.email = 'justin.kitagawa@test.com';
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 = 'justin.kitagawa@test.com';
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 = 'justin.kitagawa@test.com';
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 = 'justin.kitagawa2@test.com';
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 ()
{
Test.StartTest();
//set mock
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator1());
Database.executeBatch(new FocusDomainBatch());
Test.StopTest();
}
}