2

I've written a service class that sends emails based on a trigger. I've also written a test class that tests that code. All lines are covered.

Here is my problem. How do I run an assert to ensure that the right amount of emails were sent to the right people? (not all the records will cause an email to be sent).

I've put some code underneath so you can get an idea of how the application flows.

Trigger

trigger NotificationTriggers on Notification__c (after insert) {

    Domain.triggerHandler(NotificationDomain.class);
}

Service Class

public without sharing class NotificationService {

    public static void sendCriticalEmails(List<Notification__c> notifications) {
        List<Messaging.SingleEmailMessage> messagesToSend = new List<Messaging.SingleEmailMessage>();


        Id groupId = PublicGroupUtil.getOrCreatePublicGroup(CONSTANTS.NOTIFICATION_PUBLICGROUPNAME, false);
        EmailTemplate template = [select Id from EmailTemplate where DeveloperName = :CONSTANTS.NOTIFICATION_TEMPLATECRITICALNAME LIMIT 1];

        if (groupId != null && template != null) {
            List<User> recipients = PublicGroupUtil.getPublicGroupMembers(groupId);

            for (Notification__c notif : notifications) {

                messagesToSend.addAll(EmailUtils.createEmail(notif.id, recipients, template.id));
            }

            Messaging.sendEmail(messagesToSend);
        }

    }

}

Test Class

@isTest
public class NotificationTriggersTest {

    private static Boolean isNotSetup = true; // Used to determine if the setup method has been called by other setups

    @TestSetup
    public static void setup() {
        if (isNotSetup) {


            isNotSetup = false;
        }

        system.assert(true);
    }

    @isTest
    static void testSendEmail() {
        User u = UserTest.createAdminUser();

        System.runAs(u) {
            Id groupId = PublicGroupUtil.getOrCreatePublicGroup(CONSTANTS.NOTIFICATION_PUBLICGROUPNAME, true);

            if (groupId != null) {
                PublicGroupUtil.addUserToGroup(groupId, u.id);
            }

            Test.startTest();

            List<Notification__c> notifications = new List<Notification__c>();

            notifications.add(new Notification__c(Title__c = 'Test Notification Send 1', Status__c = 'Open', IsCritical__c = true, Type__c = 'Critical'));
            notifications.add(new Notification__c(Title__c = 'Test Notification Send 2', Status__c = 'Open', IsCritical__c = true, Type__c = 'Critical'));
            notifications.add(new Notification__c(Title__c = 'Test Notification Not Send 1', Status__c = 'Open', IsCritical__c = false, Type__c = 'Regular'));
            notifications.add(new Notification__c(Title__c = 'Test Notification Not Send 2', Status__c = 'Resolved', ResolutionDate__c = DateTime.now().AddDays(-21), IsCritical__c = true));
            notifications.add(new Notification__c(Title__c = 'Test Notification Not Send 3', Status__c = 'Resolved', ResolutionDate__c = DateTime.now().AddDays(-21), IsCritical__c = false));

            insert notifications;

            system.assertEquals(5, notifications.size());

            //Should be sending 2 emails


            Test.stopTest();
        }
    }

}
3

All you can do is test the number you send. The Limits class contains a method called getEmailInvocations.

Test.startTest();
    //do stuff
    Integer invocations = Limits.getEmailInvocations();
Test.stopTest();    
system.assertEquals(expectedValue, invocations, 'message');

Any further testing you do would have to be on a method that returns the Messaging.SingleEmailMessage instances, upon which you can make assertions about recipients, content, etc.

  • This got me real close.... but I am in a trigger-context. I can't just write a method that spits out how many records are handled or check a properly... Ideas? – Sebastian Kessel Feb 29 '16 at 20:57
  • Forget it. I took a different approach (one method to verify that the email is sent, one that verify that it isn't). I can handle the recipient information in a separate test case – Sebastian Kessel Feb 29 '16 at 21:07

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.