Here is my dilemma, I have a Schedulable Class that is set up to send an email reminder on a Task. I have two solutions; (1) The Schedulable Class that includes the Messaging.SingleEmailMessage method, and (2) The Schedulable Class that does not include the Messaging.SingleEmailMessage method, but rather uses the utility MailerUtils.sendMail for the Messaging.SingleEmailMessage method.

For Solution (1) My Test Class results in 20% coverage, however, when I use both the Schedulable Class along with the MailerUtils.sendMail Class my Test Class results in 85% coverage and 100%, respectively.

Would appreciate any direction you can provide to help me solve this.

Here is my Test Class for both solutions (I have intentionally left out asserts, as it doesn't seem to effect code coverage %. I will add in asserts once I get the code coverage % needed). If asserts do help with Code Coverage % please let me know.



private class TestEmailReminderClass { public static TestMethod void Test(){

String CRON_EXP = '0 0 0 15 3 ? 2022';  

// Schedule the test job
String jobId = System.schedule('This is a Test',
                new TaskEmailReminder());

//Create and insert a task
    Task tsks = new Task(Description = 'Task Description', Subject = 'Task Subjeck', Status = 'Not Completed', Turn_On_Email_Reminder__c = true);
    insert tsks;    

List<Task> listTasks = new List<Task>();
listTasks = [select id, ownerID, task.owner.name, task.owner.email, task.what.name, description, subject, activityDate, status from task where activityDate = tomorrow];

    for(Task tsk : listTasks)
      System.assertEquals(tsk.Subject, 'Task Subject');  


String message = 'This is my message';

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'me@email1.com','you@email2.com'};

mail.setSubject('My Subject');

// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  


Here is Schedulable Class; Solution (1) with Code Coverage:

enter image description here

Here is Schedulable Class along with the MailerUtils Class; Solution (2) with Code Coverage for each class:

enter image description here

enter image description here

I basically am trying to figure out how do I combine the two to get the necessary code coverage OR is there a solution that will allow me to pass tsk.owner.email into the utility.

Thank you!

  • 2
    Did you remember to create your test data, i.e. did you create Tasks that meet the Scheduled job's criteria? Dec 18, 2015 at 22:22
  • @ProgrammableMedley That was it. Did not have the criteria consideration. Thank you! I will post final code at 100%.
    – tomc0920
    Dec 19, 2015 at 0:32
  • 1
    @tomc0920 As an aside, you should use setTargetObjectId if possible. Unlike sending by email address, sending to user IDs are unlimited.
    – sfdcfox
    Dec 19, 2015 at 1:39

1 Answer 1


Your testmethod solution has elements that don't belong. While you didn't post your actual PROD code (only screen shot), the prod code has two methods that need to be tested

Your solution 1

`execute()` that calls `setSendEmailReminder()`

To get code coverage and the all important asserts, you simply need to

// mock some Tasks
TaskEmailSendReminder schCls = new TaskSendEmailReminder();
// asserts go here ..but what to assert?

Since the actual email doesn't get sent in a testmethod, it is hard to know if you constructed it accurately or even sent it. Hence, a useful practice is for the setSendEmailReminder method to also write to a custom Log__c object or to insert a Task somewhere in a known place and then the testmethod can query the persistent "log" and compare against the mocked data. This has the side benefit of a queryable location you can look at to diagnose issues with your production scheduled class

The above may lead you to refactor your code a bit so the contents (or excerpt) of what is mailed can be persisted somewhere.

Note that testing that the SFDC scheduler works is not really necessary in a testmethod.

One last thing -- you should take advantage of Messaging.reserveSingleEmailCapacity(..) method to handle the (low) daily outbound email limit - if you can't reserve (caught exception), reschedule your class for the next day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.