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
Madhu mMadhu m 

How to right test class to cover email logic in finish method of a batch class?

Below code is covered as I'm not sure how to fail the batch and move it to else part to do code coverage 

public void finish(Database.BatchableContext bc)

    {        
        AsyncApexJob a = [Select Id, Status, NumberofErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email, ExtendedStatus
                            from AsyncApexJob
                           Where Id =:bc.getJobId()];
         String batchName = 'test_InvalidatingAsmtRequestBatch';                
        If(a.Status=='Completed' && a.NumberofErrors==0){
             Database.executeBatch(new BCSA_BatchExtractSCRAData(),50);                      
           }
        else{ 
             test_Utility bu = new BCSA_Utility();      
             bu.sendEmailNotificationOnBatchJobFail(batchName, a.Status, a.TotalJobItems, a.NumberOfErrors,a.ExtendedStatus);
             }  
      } 
 }
 
Raj VakatiRaj Vakati
By defaul it will cover if your code contains the batch database.executeBatch method on test.starttest and test.stoptest