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
Sochy_EisenbergSochy_Eisenberg 

Unit Test for AsyncApexJob Class

I have a Schedulable class that is designed to run every hour and notify the Admin of any apex job errors so they can be addressed. I am unsure how to write the unit test since the AsyncApexJob object is read-only. Is there some kind of mockup I would use in order to get AsyncApexJob records in? Below is the my code so far. 
 
public class JobMonitorSchedulable implements Schedulable {
    
    public void execute(SchedulableContext ctx) {
     	
        List<String> emailTo = new List<String>();
        for(String email : Label.Apex_Job_Monitor_Recipient.Split(',')) {
            emailTo.add(email);
        }
        
        String subject       	= 'Apex job Failures';
        String emailBody 		= '';
        DateTime interval = DateTime.now().addMinutes(-65);
        
        // Query the failed jobs
        List<AsyncApexJob> failedJobs = [SELECT ApexClass.Name, Status, NumberOfErrors, JobItemsProcessed,
                                        TotalJobItems, CreatedBy.Name, ExtendedStatus, CreatedDate
                                        FROM AsyncApexJob WHERE ExtendedStatus != null AND CreatedDate > :interval];
        // Add columns to csv file header
        String csvHeader = 'Created By, Apex Class, Created Date, Status, Number of Errors, Total Job Items, Extended Status \n';
		String finalstr = csvHeader;            
        // Add failed job info to string
        if(!failedJobs.isEmpty()) {
            for(AsyncApexJob j : failedJobs) {
                emailBody += (j.CreatedBy.Name + ' ' + j.ApexClass.Name + ' ' + j.CreatedDate.format() + ' ' + 
                              j.Status + ' '+ j.NumberOfErrors + '/' + 
                              j.TotalJobItems + ' ' + j.ExtendedStatus + + '\n');
                
                String recordString = j.CreatedBy.Name + ',' + j.ApexClass.Name + ',' + j.CreatedDate.format() + ',' + 
                              j.Status + ','+ j.NumberOfErrors + ',' + 
                              j.TotalJobItems + ',' + j.ExtendedStatus + '\n';
                finalStr = finalStr + recordString;
            }
            
            // Add csv attachment
            Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
            blob csvBlob   = Blob.valueOf(finalstr);
            string csvname = 'Failed Apex Jobs.csv';
            csvAttc.setFileName(csvname);
            csvAttc.setBody(csvBlob);
            
            // Send email with failed jobs
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(emailTo);
            mail.setSubject(subject);
            mail.setPlainTextBody(emailBody);
            mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
            
            Messaging.SendEmailResult[] results = Messaging.sendEmail(
                new Messaging.SingleEmailMessage[] { mail });
        }
    }

}