You need to sign in to do that
Don't have an account?
Test coverage of trigger that sends single e-mail
I'm new to the APEX development world, though I have quite a bit of experience using the API. I feel like I'm very close to getting this to work, I just want to increase my test coverage of my trigger. The trigger has two tasks:
1. After the record has been inserted, a field should be updated
2. Send an e-mail
The trigger is pretty simple:
trigger UpdateMostRecentCommentOnCase on CaseComment (after insert) { Case relatedCase; Contact relatedCaseContact; Messaging.Singleemailmessage customerEmail; for (CaseComment caseComment : trigger.new) { //Find the related case. relatedCase = [SELECT Id FROM Case WHERE Id = :caseComment.ParentId]; //If the case was found, updated the most recent comment. if (relatedCase != null) { relatedCase.Most_Recent_Case_Comment__c = caseComment.CommentBody; update relatedCase; if (caseComment.IsPublished == true) { //Fetch the related case contact. relatedCaseContact = [SELECT Email FROM Contact WHERE Id = :relatedCase.ContactId]; if (relatedCaseContact != null && relatedCaseContact.Email != null) { customerEmail = new Messaging.Singleemailmessage(); customerEmail.setToAddresses(new List<String> { relatedCaseContact.Email }); customerEmail.setReplyTo('foo@bar.com'); customerEmail.setSubject('New comment for case ' + relatedCase.CaseNumber); customerEmail.setTemplateId('myActualIdIsHere'); Messaging.sendEmail(new List<Messaging.Email> { customerEmail }); } } } } }
The test class that I have is very simple as well, though, I am not entirely sure how to test e-mails with the test methods:
@isTest private class TestCase { static testMethod void testUpdateMostRecentCommentOnCase() { Case testCase; CaseComment testCaseComment; Contact testContact; Contact testQueriedContact; Messaging.Singleemailmessage testEmail; List<Messaging.Sendemailresult> testEmailResults; testContact = new Contact(); testContact.FirstName = 'Foo'; testContact.LastName = 'Bar'; insert testContact; testCase = new Case(); testCase.Subject = 'Test Case'; testCase.ContactId = testContact.Id; insert testCase; test.startTest(); testCaseComment = new CaseComment(); testCaseComment.ParentId = testCase.Id; testCaseComment.CommentBody = 'Test Case Comment'; insert testCaseComment; //What else do I need here to test? test.stopTest(); } }
This only puts me at 52% code coverage; everything after the comment, "//Fetch the related case contact." is not covered.
How do I provide coverage for the e-mail piece of the trigger?
Thanks for any input!
Hi
Try this
@isTest
private class TestCase {
static testMethod void testUpdateMostRecentCommentOnCase() {
Case testCase;
CaseComment testCaseComment;
Contact testContact;
Contact testQueriedContact;
Messaging.Singleemailmessage testEmail;
List<Messaging.Sendemailresult> testEmailResults;
testContact = new Contact();
testContact.FirstName = 'Foo';
testContact.LastName = 'Bar';
testContact.Email ='jdoe_test_test@doe.com';
insert testContact;
testCase = new Case();
testCase.Subject = 'Test Case';
testCase.ContactId = testContact.Id;
insert testCase;
test.startTest();
testCaseComment = new CaseComment();
testCaseComment.ParentId = testCase.Id;
testCaseComment.IsPublished = true;
testCaseComment.CommentBody = 'Test Case Comment';
insert testCaseComment;
//What else do I need here to test?
test.stopTest();
}
}
Regards,
Rajesh.
All Answers
set the casecomment is published == true before inserting the casecomment
Also, you will want to add some system asserts to check and make sure that values changed appropriatly
As for the email, all you cannot test if the email actually went.
Hi
Try this
@isTest
private class TestCase {
static testMethod void testUpdateMostRecentCommentOnCase() {
Case testCase;
CaseComment testCaseComment;
Contact testContact;
Contact testQueriedContact;
Messaging.Singleemailmessage testEmail;
List<Messaging.Sendemailresult> testEmailResults;
testContact = new Contact();
testContact.FirstName = 'Foo';
testContact.LastName = 'Bar';
testContact.Email ='jdoe_test_test@doe.com';
insert testContact;
testCase = new Case();
testCase.Subject = 'Test Case';
testCase.ContactId = testContact.Id;
insert testCase;
test.startTest();
testCaseComment = new CaseComment();
testCaseComment.ParentId = testCase.Id;
testCaseComment.IsPublished = true;
testCaseComment.CommentBody = 'Test Case Comment';
insert testCaseComment;
//What else do I need here to test?
test.stopTest();
}
}
Regards,
Rajesh.
Good catch. Need to place a value in the contact email field as well.
I can't believe I overlooked those fields not having a value. Thank you both for your help, I appreciate it.