You need to sign in to do that
Don't have an account?
Harjeet Singh 13
Test Class SingleEmailMessaging
Hi Everyone,
I need a test class code which covers the below lines of code:
I am unable to cover the code for exception mail code
Kindly help me
Thanks & Regards,
Harjeet
I need a test class code which covers the below lines of code:
I am unable to cover the code for exception mail code
/* @purpose : Send the exception email to the user who started the batch job */ public static void sendExceptionEmail(Exception e) { Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setTargetObjectId(UserInfo.getUserId()); message.subject = 'Batch job failed to create Pickup request on ' + System.now().format('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> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); }
Kindly help me
Thanks & Regards,
Harjeet
Now what i would suggest that you can add below test method in your existing test class and replace the class "EmailServiceUtil" with your class name where method exist "sendExceptionEmail()". In this way, overall you will get coverage for this code as well.
@IsTest
static void testSendExceptionEmail() {
DMLException dmlEx = new DMLException('Test Ex Email');
EmailServiceUtil.sendExceptionEmail(dmlEx);
// Just repace "EmailServiceUtil" with your class name.
}
Regards,
Pawan Kumar
All Answers
It is always difficult to teset email. So You will have to create separte class for your email and then write 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.setTargetObjectId(UserInfo.getUserId());
message.subject = 'Batch job failed to create Pickup request on ' + System.now().format('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 > {
message
};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
}
}
// Test Class
@IsTest
Public class EmailServiceUtilTest {
@IsTest
static void testSendExceptionEmail() {
DMLException dmlEx = new DMLException('Test Ex Email');
EmailServiceUtil.sendExceptionEmail(dmlEx);
}
}
Please let me know if it helps you.
Regards,
Pawan Kuar
But the thing is I can't write separate class for email class. sendExceptionEmail method is a part of class and the class was written by some other developer and I am trying to increase the code coverage. But not able to cover a single line of the sendExceptionEmail method
Kindly let me know how can I include the test coverage for the same in one test class where I wrote the test method for other class methods
Thanks & Regards,
Harjeet
Now what i would suggest that you can add below test method in your existing test class and replace the class "EmailServiceUtil" with your class name where method exist "sendExceptionEmail()". In this way, overall you will get coverage for this code as well.
@IsTest
static void testSendExceptionEmail() {
DMLException dmlEx = new DMLException('Test Ex Email');
EmailServiceUtil.sendExceptionEmail(dmlEx);
// Just repace "EmailServiceUtil" with your class name.
}
Regards,
Pawan Kumar
Really I need a help
Thanks & Regards,
Harjeet
Thanks pawan you resolved my issue. Thanks a lot.
Actually I didnt mentioned testMethod instead simply copied pasted your code and replace with my code. Once I add testMethod keyword and ran MAGIC happened. All the lines of the codes got covered.
I really appreaciate your kind help
Thanks & Regards,
Harjeet
@IsTest
static void testDocumentQuoteEmailController() {
PageReference pageref = new PageReference();
DocumentQuoteEmailController.sendDoc(pageref);
// Just repace "EmailServiceUtil" with your class name.
}