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
learning_SFDClearning_SFDC 

Test coverage 100% for schedule class but 18 for Batch

hi 

I have written test class for batch apex and schedule class my schedule class is 100 but batch only 18 please help me in code coverage:-

Batch class:-
 
global class Del_Email implements Database.Batchable<sobject> ,Database.Stateful
{
   global String query;
   global String email_hdr;
   global String email_reslts;
   global integer email_count;  
   global boolean send_email;
   global date ext_date=System.today().addDays(-14);  
  
  public Del_Email(string q)
  {
    this.query=q;
    email_hdr='Id,Name,Subject Line\n';
    email_reslts=email_hdr;
    email_count=0;
    send_email=false;   
    
  }
   global Database.QueryLocator start(Database.BatchableContext BC){
      system.debug(query);
      return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext BC,List<sobject> recs)
   {
    
        string temp;
        List<xtma_Individual_Email_Result__c> delIer=new List<xtma_Individual_Email_Result__c>();
        for(sobject s:recs)
        {
            xtma_Individual_Email_Result__c Ier=(xtma_Individual_Email_Result__c)s;         
            string name =Ier.Name==null?'':string.ValueOf(Ier.Name).replaceAll(',','');
            
            
            if(temp==null)temp=' ';         
            temp+=Ier.id+','+name+','+sub+'\n';
            delier.add(ier);        
        }
        if(temp!=null)
        {
            try{
                Delete delier;
                email_reslts+=temp;
                send_email=true;                
            }   
            catch(Exception e)
            {
                system.debug(e);
            }   
            if(Blob.valueOf(email_reslts).size()>=8388608)
            {
                sendmail(email_reslts,email_count);
                email_reslts=email_hdr;
                send_email=false;
            }
        }
    
   }  
   
   global void finish(Database.BatchableContext BC){        
        System.debug(LoggingLevel.WARN,'Deleting Individual Email Results Finished');
        if(send_email && email_reslts!=email_hdr)
        {
            sendmail(email_reslts,email_count);
        }
   }
   
   public void sendmail(string mailbody,integer mailcount)
   {
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName('Email Results'+System.today().day()+'_'+System.today().Month()+'_'+System.today().Year()+'_'+mailcount+'.csv');
            Blob b=Blob.valueOf(mailbody);
            efa.setContentType('text/csv');             
            efa.setBody(b);       
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            mail.setToAddresses(new String[] {'test@gmail.com'});        
            mail.setSenderDisplayName('Batch Processing');
            mail.setSubject('Batch Process Completed - '+system.today());
            mail.setPlainTextBody('Please find the attachment of deleted records, dated : '+system.today());            
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});     
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            email_count++;  
   }
   
}




Execute and mail method is not covering in test class


Schedule class:-
global class bulk_delete_emails Implements Schedulable{

public static String CRON_EXP = '0 0 0 3 9 ? 2022';
/*Method to query for the records to delete */

     global void Execute(SchedulableContext SC){             
        string query='select id,name,Subject_Line__c from xtma_Individual_Email_Result__c where Date_Time_Sent__c<=:ext_date limit 20 ';            
        Del_Email der=new Del_Email(query);        
        database.executebatch(der);         
     }     
}


Test Class:-
@IsTest
private class bulk_delete_individual_emails_Test{
 static testmethod void bulk_delete_individual_emails() {   
   
    date ext_date=System.today().addDays(-14);  
    set<id>ids=new set<id>(); 
    

     List <xtma_Individual_Email_Result__c> lds = new List<xtma_Individual_Email_Result__c>();
            for(integer i = 0; i<5; i++){
             
                xtma_Individual_Email_Result__c l = new xtma_Individual_Email_Result__c(); 
                l.name='anil';
                l.Date_Time_Sent__c=system.today();
                l.Date_Bounced__c=system.today();
                l.Date_Unsubscribed__c=system.today();
                l.Date_Time_Opened__c=system.today();               
                lds.add(l);
                
            }    
        insert lds;       
       
       Test.startTest(); 
       string query='select id,name,Subject_Line__c from xtma_Individual_Email_Result__c where Date_Time_Sent__c<=:ext_date   limit 200 ';
       Del_Email c = new Del_Email (query);
       Database.executeBatch(c);
 Test.stopTest();

  

   }
   }