I am using the EmailManager provided in Trailhead and I am writing a test class and it works as expected (and I get over 75% coverage) but I can't seem to get it to 100% coverage because of the sendResult = False in the EmailManager Class. How can I set up the test class to get 100% coverage?


public class EmailManager
   public static void sendMail(String address, String subject, String body) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });

    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;

        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
         return sendResult;


Private class EmailManager_Test
    static string address;
    static string subject;
    static string body;
    static boolean sendResult;

    static void init()
       address = 'noreply@email.com';
       subject = 'Test Email for Test Class';
       body = 'This is a Test Email for Test Class';

    public static testMethod void Test_sendMail()
        EmailManager.sendMail(address, subject, body);
  • Try a new test method where you pass null as the address and/or subject/body. The email system should fail if the address is null or not valid which theoretically could cause what you're looking for. It could also just throw an exception though. – gNerb Jun 21 '18 at 15:30
  • Thanks for the comment. When I do that it does not like the true side of the equation. I was curious if there is a way to test for both conditions in one test. I thought there was a way to do it but I could not make it happen. – Daryn Jun 21 '18 at 16:26
  • You can not achieve that in this code sample. sendMail() is supposed to create only one instance of SingleEmailMessage. Generally, you should prefer that each test method tests a single logical outcome. Email was send successfully would be the first logical outcome, Errors occurred would be the second one. – Michal Vavra Jun 21 '18 at 20:32
  • Thank you for your time. At least I know I am not the only one that could not solve since it seems unsolvable. However, I know this community has many bright people and thought I would see if anyone had a solution or even a creative one. I was originally only going to accept an answer if could get it too 100% but I reasonable answer is also one where it can't be solved. If you repost you answer I will be happy to accept. – Daryn Jun 22 '18 at 12:06
  • @Daryn , the trick to effective test is to cover all code paths. If you cannot cover with a single method, Test_sendMail(), you create another test method for an uncovered path. In your case, you can have one more failing address variable which is set to a null or invalid address and call sendMail() twice once with a valid email address and another with invalid. Once with a valid address and once with an invalid in the same test. However, it's not reusable software work. Recommend think of each test to cover a logical unit/path of your work. An effective way is to create more test methods. – Vijaya Kapadam Jul 15 '18 at 2:14

Your Answer

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

Browse other questions tagged or ask your own question.