You need to sign in to do that
Don't have an account?

Test class not covering for execute method
I have a batch class and I'm trying to write a test class for that batch class but the test class is not covering the execute method. Could someone please help me on this.
global class ACMContactDocumentExpiration implements Database.Batchable<sObject>,database.stateful { public List<Aircraft_Training__c> AirTrn = new List<Aircraft_Training__c>(); public List<Crew_Staff__c> Crewlist=new List<Crew_Staff__c>(); public List<Experience__c> Exp = new List<Experience__c>(); public List<Training__c> Trn = new List<Training__c>(); public List<Contact> Con = new List<Contact>(); global Database.QueryLocator start(Database.BatchableContext BC) { AirTrn = [Select Id, Name, Experience__c,Experience__r.Name, Aircraft_Rating__c, PPC_Expiration_Date__c, PPC_Date__c, Training_Date__c FROM Aircraft_Training__c WHERE PPC_Expiration_Date__c =: Date.today()+Integer.valueOf(System.label.AL_PPC_Expiration_Days)]; Set<String> AirTrnIds = new Set<String>(); for(Aircraft_Training__c a: AirTrn) { AirTrnIds.add(a.Experience__r.Name); } system.debug('----------'+AirTrnIds.size()); Exp = [Select Id, Name, Contact__c, Contact__r.Id, Last_Recurrent_Date__c FROM Experience__c WHERE Name IN: AirTrnIds AND (Last_Recurrent_Date__c =: Date.today()-Integer.valueOf(System.label.Exp_Last_Recurrent_Days) OR Last_Recurrent_Date__c != null OR Last_Recurrent_Date__c =: null)]; Set<String> ExpIds = new Set<String>(); for(Experience__c e: Exp) { ExpIds.add(e.Contact__r.Id); } system.debug('----------'+ExpIds.size()); Trn = [Select Id, Name, Contact__c, Contact__r.Id, Expiration_Date__c, Exam_Date__c, Training_Type__c FROM Training__c WHERE Expiration_Date__c =: Date.today()+Integer.valueOf(System.label.Training_Expiration_Days) AND (Training_Type__c =: System.label.Training_Type_Intl_Procedures OR Training_Type__c =: System.label.Training_Type_RVSM)]; Set<String> TrnIds = new Set<String>(); for(Training__c t: Trn) { TrnIds.add(t.Contact__r.Id); } system.debug('----------'+TrnIds.size()); Con = [Select Id, Email FROM Contact WHERE ID IN: ExpIds OR ID IN: TrnIds]; Set<String> ConIds = new Set<String>(); for(Contact c: Con) { ConIds.add(c.Id); } system.debug('----------'+ConIds.size()); string query= 'Select Id, Contact__c, Contact__r.Name, Active_License__r.License_Type__c, Active_Medical__r.Medical_Type__c, Mandate_Record_Type__c, Contact_Record_Type__c, License_Expiration_Date__c, Medical_Expiration_Date__c,Training_Expiration_Date__c, Crew_Email__c from Crew_Staff__c WHERE Contact__r.Id IN : ConIds'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Crew_Staff__c> scope) { List<Crew_Staff__c> newCrew = new List<Crew_Staff__c>(); Set<Id> newCrewIds = new Set<Id>(); for(Crew_Staff__c crew:scope) { if( ((crew.Contact_Record_Type__c == 'Pilot') && (crew.Mandate_Record_Type__c == 'ACM') && (crew.Crew_Email__c != null)) || (date.valueof(crew.License_Expiration_Date__c) == System.today()+Integer.valueOf(System.label.Date_2)) || (date.valueof(crew.Medical_Expiration_Date__c) == System.today()+Integer.valueof(System.label.Medical_Expiration_Days)) ) { Crewlist.add(crew); } newCrewIds.add(crew.Id); } system.debug('----------'+Crewlist.size()); } global void finish(Database.BatchableContext BC) { system.debug('------>'+Crewlist.size()); List<Messaging.SingleEmailMessage> maillist = new List<Messaging.SingleEmailMessage>(); if(Crewlist.size()>0) { for(Crew_Staff__c crewrec:Crewlist) { // Query on OrgWideEmailAddress would set "FROM address" in the email OrgWideEmailAddress[] owea = [Select Id From OrgWideEmailAddress where Address = 'test@test.com']; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); if (owea.size()>0) { mail.setOrgWideEmailAddressId(owea.get(0).Id); } mail.setSubject('Pilot Documents Expiring Soon'); mail.setToAddresses(new String[]{crewrec.Crew_Email__c}); mail.setReplyTo('flightops@acass.ca'); string htmlBody; htmlBody = '<p><span style=font-family:calibri, monospace> Dear '+ crewrec.Contact__r.Name +', </span> </p>' + '<p><span style=font-family:calibri, monospace> This is a friendly reminder that you have the following documents coming due for renewal: </span></br>'; String lType = crewrec.Active_License__r.License_Type__c; Date lExpDate = date.newInstance(crewrec.License_Expiration_Date__c.year(), crewrec.License_Expiration_Date__c.month(), crewrec.License_Expiration_Date__c.day() ); String mType = crewrec.Active_Medical__r.Medical_Type__c; Date mExpDate = crewrec.Medical_Expiration_Date__c; htmlBody += +lType+ ' license will expire on ' +lExpDate+'</br>'; htmlBody += +mType+ ' medical will expire on ' +mExpDate+'</br>'; for(Training__c t: Trn) { String tType = t.Training_Type__c; Date tExpDate = t.Expiration_Date__c; htmlBody += +tType+' training will expire on ' +tExpDate +'</br>'; } for(Aircraft_Training__c a1: AirTrn) { String aName = a1.Aircraft_Rating__c; Date aPPCexp = a1.PPC_Expiration_Date__c; htmlBody += +aName+' aircraft training will expire on ' + aPPCexp +'</br>'; } htmlBody += '<p><span style=font-family:calibri, monospace> Please send clear, colour photos/scans the renewed document prior to the expiration date. </span></p>'+ '<p><span style=font-family:calibri, monospace>Kindest regards,</span></br>'+ '<span style=font-family:calibri, monospace>Flight Operations </span></p>'; mail.setHtmlBody(htmlBody); maillist.add(mail); } Messaging.sendEmail(maillist); } }
@isTest public class SendEmailToCrewJobscheduledtest { public static testmethod void myUnitTest() { List<Contact> con = new List<Contact>(); Contact c = new Contact( Accident_Note__c = 'Test',FirstName = 'Test',LastName = 'test'); con.add(c); insert con; Test.startTest(); String CRON_EXP = '0 0 0 3 9 ? 2022'; String jobId = System.schedule('SendEmailToCrewJobscheduledtest', CRON_EXP, new SendEmailToCrewJobscheduled()); CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; System.assertEquals(0, ct.TimesTriggered); System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime)); ACMContactDocumentExpiration b = new ACMContactDocumentExpiration(); Database.QueryLocator q1 = b.start(null); Database.executeBatch(b, 200); Test.stopTest(); } }
try to remove below line and run the test class;
Database.QueryLocator q1 = b.start(null);
Thanks
Shivdeep