You need to sign in to do that
Don't have an account?
Naomi 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:
Test Class (my test load data information is listed below the code snippet):
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??
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??
All Answers
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...
Thanks,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...
Nagendra