+ Start a Discussion

Test class for Messaging class

Hey guys-


I've got a simple messaging class that works great, but I need a solid test class for it.


All this is is a class that's called from a javascript custom button that is on the page layout of a custom object record.  When someone click the button, it calls this class, and an email is sent out to the email you can see in the class (I've changed it for anonymity here to 'test@test.com')


global class CIFEmails 
    WebService static void SendEmailNotification(string id, string accountName) 
    //create a mail object to send a single email.
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    //set the email properties
    mail.setToAddresses(new string[] {'test@test.com'});
    mail.setSenderDisplayName('Ping Salesforce');
    mail.setSubject('A CIF Has Just Been Submitted to Contracts & Licensing for: ' + accountName  );
    mail.setHtmlBody('A Customer Information Form has just been Submitted to Contracts & Licensing.'+  
                     'To access the CIF, please go to the record in Salesforce, here: '+ 
                     'https://cs15.salesforce.com/' + id);
    //send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

 So, I need no test class for my javascript button.  But I do need a test class for the messaging class above.


Problem I see is the SF developer documentation is lacking on how to test emails sent via apex.  


Anyone have any code I could take a look at that is a GOOD test class for this class?  Doesn't have to be bulkified in this case since the class is only ever called from a custom button.  


Thanks a lot for any help anyone can provide




private static void testCIFEmails() {


 will do code coverage; Apex testmethods will not send emails (for obvious reasons - to avoid spamming folks).




Thanks.  Yeah that passes code coverage, but if there's a way to write the test class to actually test the values in the email subject, to address, and body, that would be great.  I'm working on this myself but if anyone knows how to do that or can give me a sample to work with I'd really appreciate it.




There don't appear to be any Apex inspector methods (getXXX) on the SingleEmailMessage object. Other solutions would have you build an intermediary object in the webservice method that you could do asserts on; then copy the values from the intermediary object into the SingleEmailMessage object but I'm not sure what the point would be

Faizan Nusrat 12Faizan Nusrat 12
May this code help you .  Always cover the Contact email in test coverage .

Try this
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;
        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?