function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naomi HarmonNaomi Harmon 

Fix Web Service Callout error in Batch Apex Class and Test Class

I am trying to create a batch Apex class that I can schedule to run weekly to automatically unqualify Leads that are 60 days old or older and haven't had activity on them in the past 10 days (and only for a single Record Type). Here is my Class and Test Class...I keep receiving a "Methods defined as TestMethod do not support Web service callouts." My test class doesn't include any callouts..but I know we have a trigger or two in our instance on Leads that does have a callout. How do I fix my test class then?

Batch Apex Class:
global class AutoUnqualifyLeads implements Database.Batchable<sObject> {
   
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id, Status, Reason_Not_Qualified__c, LastActivityDate '+
                         'FROM Lead '+
                        'WHERE CreatedDate < LAST_N_DAYS:60 '+
                          'AND Days_Since_Last_Activity__c > 10 '+
                          'AND IsConverted = false '+
                          'AND Status != '+' \'Unqualified\' '+
                          'AND RecordTypeId = '+' \'01270000000Q4tB\' ';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        List<Lead> leads = new List<Lead>();
        for (Lead l: scope){
                l.Status                  = 'Unqualified';
                l.Reason_Not_Qualified__c = 'No Response';
                system.debug(l);
                leads.add(l);
        }
   update leads;
   }

    global void finish(Database.BatchableContext bc){
        
    }
}

Test Class (my test load data information is listed below the code snippet):
@isTest
public class AutoUnqualifyLeadsTest{
        
    @testSetup
    static void setupTest(){
        List<Lead> leadLoad = Test.loadData(Lead.sObjectType,'testLeadsAutoUnqual');
        System.debug(leadLoad);
        
        Lead L1 = (Lead)leadLoad[0];
        
        List<Task> tasks = new List<Task>();
         Task t = new Task();
           t.Subject = 'Test';
           t.Status = 'Completed';
           t.ActivityDate = Date.newInstance(2017, 10, 09);
           t.WhoId = L1.Id;
           System.debug('WhoId = '+t.WhoId);
         tasks.add(t);
         insert tasks;
        
        L1.Status = 'Contacted';
        Update L1;
             
        List<Lead> days = [Select Days_Since_Last_Activity__c, Status, FirstName 
                             From Lead
                            Where Id = :leadLoad];
        System.debug(days);
        
        
    }

    @isTest
    static void testLeads(){
        
        Test.startTest();
        AutoUnqualifyLeads aul = new AutoUnqualifyLeads();
        Id batchId = Database.executeBatch(aul);
        Test.stopTest();
        
     //after the testing stops, assert records were updated properly
     System.assertEquals(1, [select count() from Lead where Reason_Not_Qualified__c='No Response']);
    }
 
}

Test data:

CreatedDate, RecordTypeID, Days_Since_Last_Activity__c, Status, Reason_Not_Qualified__c, LastName, Company
2017-10-07T01:40:39, 0121I000000QxE7, 12, Open, Bing, Friends Co.
2017-09-22T01:43:39, 0121I000000QxE7, 13, Contacted, Gellar, Friends Inc.
2014-05-02T02:43:39, 0121I000000QxE7, 4, Open, Green, Friends LLC

Can you please help me troubleshoot??
Best Answer chosen by Naomi Harmon
Naomi HarmonNaomi Harmon
Because of other triggers in my instance that make callouts when a record is created, I had to insert a Test.setMock(); statement in my test class in order to get it work.

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Harmon,

May I suggest you please check with the solution suggested in the stack exchange community for a similar issue.
  • Feels like you have the wrong annotation on line 4...
    If that is where you are setting the leads, the annotation should be @testSetup to load the leads so that the testLead methods have some test data.
    class testLeads on line 12 has no data to test, that is why your assertion is failing...
Thanks,
Nagendra
 
Naomi HarmonNaomi Harmon
Thank you for the response, but my annotation on line 4 is already @testSetup, and that wasn't the question I'm trying to solve for.
Naomi HarmonNaomi Harmon
Because of other triggers in my instance that make callouts when a record is created, I had to insert a Test.setMock(); statement in my test class in order to get it work.
This was selected as the best answer