0

I am fairly new to apex testing and am completely lost on how I am supposed to test a schedulable class that regularly sends an email. The class that I have written is below. It is very simple, and its only function is to regularly send the same exact email to the same exact people everyday. Note that anywhere there is an 'X' is actually something else, I just put 'X' there for privacy reasons. Any advice on how to test this would be immensely appreciated.

public class SendSurveyEmail implements Schedulable {
public void execute(System.SchedulableContext sc){
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.setUseSignature(false); 
    message.setBccSender(false); 
    message.setSaveAsActivity(false); 
    
    EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'X'];
    message.setTemplateId(emailTemplate.Id);
    
    User[] allUsers = [SELECT Id, Email FROM User WHERE isActive=:true];
    String[] toEmails = new List<String>();
    for(Integer i = 0; i < allUsers.size(); i++){
        String email = allUsers[i].Email;
        if (email.contains('X')){
            toEmails.add(email);        
        }
    }
    message.setToAddresses(toEmails);
    
    String surveyLink = 'X';
    message.setPlainTextBody(surveyLink);
    
}
3
  • 2
    Heads-up: as written, this class doesn't do anything; there's no call to actually send the email.
    – David Reed
    Commented Nov 3, 2020 at 21:38
  • 4
    Does this answer your question? Test class for Schedulable class. Specifically see Adrian Larson's answer.
    – Phil W
    Commented Nov 4, 2020 at 8:13
  • @DavidReed Fixed that shortly after, a mistake I tend to make when writing scheduleable classes.
    – Slippy
    Commented Nov 5, 2020 at 17:02

0

Browse other questions tagged .