0

Currently I'm building a feature that use the Messaging.CustomNotification Apex class, and I'm building the corresponding test methods, but I haven't found a way to check that the notification has been sent. I was thinking use the Limit class, but I haven't found any method that works for what I need.

Is there a way to check that in an Apex Test?

2 Answers 2

1

No.. this seems to be one of the scenarios where asserts are not possible

The way notifications work -> fire and forget.

Normally in these scenarios if the try doesn't fail we assume its a success

try {
    notification.send(addressee);
}
catch (Exception e) {
    System.debug('Problem sending notification: ' + e.getMessage());
} 

there are some limits though

https://help.salesforce.com/s/articleView?id=sf.process_limits_customnotification.htm&type=5

0

In situations like these, put your custom notification send logic into a separate class - e.g. MyCustomNotificationSender.

Then, at test time, construct a mock version of the MyCustomNotificationSender called MockCustomNotificationSender with the same signature as your MyCustomNotificationSender methods.

The mock simply counts how many times it is called in a static variable (or, if using ApexMocks, it will count for you)

Then, inject the mock into the runtime version of the code-under-test. Finally, execute the code-under-test.

Your mock can also throw exceptions as part of dependency-injected constructor parameters.

You don't really need to know if the send method works as SFDC will take care of that - you should be more interested in whether you tried to send the notification and how many times

For another take on custom notifications, see the andyinthecloud blog

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .