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
Ek0nomikEk0nomik 

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!

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh SriramuluRajesh Sriramulu

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

Starz26Starz26

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.

Rajesh SriramuluRajesh Sriramulu

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.

This was selected as the best answer
Starz26Starz26

Good catch. Need to place a value in the contact email field as well.

Ek0nomikEk0nomik

I can't believe I overlooked those fields not having a value.  Thank you both for your help, I appreciate it.