You need to sign in to do that
Don't have an account?
Test coverage for email class
Hi All,
I am trying to write test coverage for the following class that sends an email. Any help would be greatly appreciated. I'm a novice and I wrote the class in less than a day. Now, I've been trying to write the test class for a week and can't get over 32% coverage.
The lines that don't have coverage are, 20, 25-28, 33, 36, 39-50, 55-57.
Please help!!!
Thanks
Bill Haberlin
________________________________________________________________
1. public class emailFunctions {
2. public Boolean sendEmail (Id caseId, String assign_to_old, String assign_to_new) {
3. Case i=[select CaseNumber, CreatedDate, Subject, Origin, Priority, Opened_By__c, Assignment_Action__c , Assign_To__c,
4. Assignment_Due_Date__c, Description, Contact.Name, Contact.Phone, Contact.MobilePhone, Contact.Email,
5. Contact.MailingStreet, Contact.MailingCity, Contact.MailingState, Contact.MailingPostalCode
6. from case where id=:caseId];
7.
8. Boolean result = true;
9.
10. // Check to see that Assign To field has been changed by user
11. if(assign_to_old != assign_to_new){
12. // Get the list of email addresses from the Assignees
13. // object, where they match the Assign To value
14. String[] userEmail = new String[]{};
15. String[] userCC = new String[]{};
16.
17. // Load the eamil addresses into a string array
18. list<Assignees__c> lstAssignees=[select email__c from assignees__c were department__c=:i.assign_to__c];
19. for (Assignees__c oAssignee: lstAssignees) {
20. userEmail.add(oAssignee.email__c);
21. }
22.
23. // Only send the email if there are email addresses that match
24. if (userEmail.size() > 0 || userCC.size() > 0) {
25. Messaging.SingleEmailMessage mail = new Mssaging.SingleEmailMessage();
26. mail.setToAddresses(userEmail);
27. mail.setSubject('Please address the following issue');
28. String body = '<font face="Candara">' +
29. '<p>A case <b>' + i.CaseNumber + '</b> was created on <b>' + i.CreatedDate.month() + '/' +
30. i.CreatedDate.day() + '/' + i.CreatedDate.year() + '</b>.</p>' +
31. '</font>';
32.
33. mail.setHtmlBody(body);
34.
35. //Upload the file
36. List<Attachment> attachments =[SELECT Id, Name, Body, ContentType FROM Attachment
37. WHERE Parentid=:caseId];
38.
39. Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[attachments.size()];
40. Integer cnt = 0;
41. for (Attachment att:attachments) {
42. if (att.Body!=NULL){
43. // Get the list of files attached to the Case and attach them to the email
44. Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
45. fileAttachment.setBody(att.body);
46. fileAttachment.setFileName(att.name);
47. fileattachment.setContentType(att.ContentType);
48. fileAttachments[cnt] = fileAttachment;
49. mail.setFileAttachments(fileAttachments);
50. cnt += 1;
51. }
52. }
53.
54. // Send the email
55. List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
56. if (!results.get(0).isSuccess()) {
57. result = false;
58. }
59. }
60. }
61. return result;
62. }
63.}
__________________________________________________________________
Hi,
Have you used @istest(seeAlldata=true) in test class??
System will not able to access organisation data if the Apex Class Version is above 24.0.
Hi Kittu,
Excellent suggestion. That brought the test coverage from 32% to 70%. That is a huge help.
Thank you so much.
Hey Bill.
Go for this post for the test classes
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html