You need to sign in to do that
Don't have an account?
Jung Mun
Hi,
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?
This is an example of a positive unit test though I wish to convert to a negative unit test.
Writing Negative Unit Test for Custom Notification Method
@isTest static void sendNotificationTest(){ Boolean exceptionThrown = false; try{ ProductHandler.sendNotification('invalidemail@0284hgwfekb.com','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'); }
Hi,
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?
@testVisible 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'); notification.setSenderId(recipientId); // Set the notification type and target notification.setNotificationTypeId(notificationType.Id); notification.setTargetId(targetId); // Actually send the notification notification.send(recipientsIds); } 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.
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.
Also, suggest reviewing this post
Let me know if it helps
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 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm
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.
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
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.