function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jung MunJung Mun 

Writing Negative Unit Test for Custom Notification Method

    static void sendNotificationTest(){
        Boolean exceptionThrown = false;
        ProductHandler.sendNotification('','Mock Product  Name',null);
        Boolean expectedExceptionThrown =  (e.getMessage().length() == null) ? true : false; 
        System.AssertEquals(true, expectedExceptionThrown, e.getMessage()); 
        } catch (Exception e){
        exceptionThrown = true;
        expectedExceptionThrown =  (e.getMessage().length() != null) ? true : false; 
        System.AssertEquals(true, expectedExceptionThrown, e.getMessage()); 
        System.assertEquals(true,exceptionThrown,'User got notified about the newly added product');

I was wondering if you can share an example of writing a negative unit test for a custom notification method. What would be the scenario in which the notification failed to send? EmailException error is generated first before Exception error. How can I get 100% code coverage?
    private static void sendNotification(String recipientId,String productName,String TargetId){
        // query custom notification for Product Exists alert
        CustomNotificationType notificationType = [SELECT id,customNotifTypeName FROM CustomNotificationType WHERE DeveloperName = 'Product_Exists'];
        // get running user ID
        Set<String> recipientsIds = new Set<String>{recipientId};
        // create a new instance of Messaging.CustomNotification class with new custom notification
        try {
        Messaging.CustomNotification notification = new Messaging.CustomNotification();
        notification.setTitle('New Product : ' + ProductName + ' is added to inventory');
        notification.setBody('New Product  : ' + ProductName + ' is added');

        // Set the notification type and target
        // Actually send the notification
        catch (Exception e) {
            System.debug('Problem sending notification: ' + e.getMessage());


This is an example of a positive unit test though I wish to convert to a negative unit test.
AnudeepAnudeep (Salesforce Developers) 
Hi Jung, 

You need to cover the exception part in the code to get full coverage

In your test method, insert records in such a way that it will cause the exception to occur. This will cover the exception lines as well.
try this in your test class.
//perform actions like insert,update,delete...etc that causes your exception to occur
Catch(exception e)

Also, suggest reviewing this post 

Let me know if it helps
Jung MunJung Mun
Hi @anudeed
thanks for your reply. Yes I understand that I have to create an instance of failing scenario. I cannot seems to find a scenario that will catch by the system.exception. Assigning Null value to sender or recipient is causing EmailException. How can I catch EmailException? I read through this documentation but doesn't seem to address catching EmailException error
AnudeepAnudeep (Salesforce Developers) 
Hi Jung, 

It is always difficult to test email. So You will have to create a separate class for your email and then write a test class in order to get code coverage as below.
// Email Class
Public class EmailServiceUtil {

 public static void sendExceptionEmail(Exception e) {
  Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
  message.subject = 'Batch job failed to create Pickup request on ' +'MM/dd/yyyy HH:mm:ss');
  message.saveAsActivity = FALSE;
  message.plainTextBody = 'Exception while creating Pickup requests ' + e.getMessage() + '\n' + e.getStackTraceString();
  Messaging.SingleEmailMessage[] messages =
   new List < Messaging.SingleEmailMessage > {
  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

// Test Class
Public class EmailServiceUtilTest {

 static void testSendExceptionEmail() {
  DMLException dmlEx = new DMLException('Test Ex Email');


Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Jung MunJung Mun

I have a send single email method in the same class and was able to get the full code coverage for it by counting invoked message. I am wondering I should do same for custom notification.