You need to sign in to do that
Don't have an account?

Test class for Custom Send an Email Button !!!
Hi EveryOne,
I have a Extensions on Oppportunity, which sends the Email with two Visualforce Pdf file attachments.
I need to write a test class for it,
As of now, My test coverage is 8%.
Please find my Code below.
Extension Code:
Can anyone please help me to write a Test class for my Extensions?
Thanks In Advance.......
Ranjith
I have a Extensions on Oppportunity, which sends the Email with two Visualforce Pdf file attachments.
I need to write a test class for it,
As of now, My test coverage is 8%.
Please find my Code below.
Extension Code:
//Send email with Multiple attachments, and before sending edit template public class SendEmail { public Account acc; public String body { get; set; } Product_EmailTemplates__c template;//Custom settings to read the template, which template to send public Opportunity opp { get; set;} public String subject { get; set; } public String htmlBody {get; set;} // Create a constructor that populates the Opportunity object public SendEmail(ApexPages.StandardController controller) { opp = [Select Id, Name, Email__c, Amount, Contact_Name__c, Account.PersonContactId, Account.id, Product__r.Name, Account.Name, StageName, Tax_Amount__c, Total_after_Tax__c, payment_1_Amount__c, Payment_2_Amount__c, Payment_3_Amount__c, Payment_1_Before_Tax__c, Payment_2_Before_Tax__c, Payment_3_Before_Tax__c, Payment_1_Tax_Amount__c, Payment_2_Tax_Amount__c, Payment_3_Tax_Amount__c, Potential_Number__c,Branch_Name__c, Branch_Name__r.Name, Branch_Name__r.Address_1__c, Branch_Name__r.Address_2__c, Branch_Name__r.City__c, Branch_Name__r.Country__c, Branch_Name__r.Main_Phone_1__c, Branch_Name__r.Main_Phone_2__c, Branch_Name__r.Main_Phone_3__c, Branch_Name__r.Region__c, Branch_Name__r.State__c, Branch_Name__r.Zip_Postal_Code__c, Product__r.Product_Type__c, Admin_Fee__c, Payment_1_plus_Admin_Fee__c from Opportunity where Id = :ApexPages.currentPage().getParameters().get('id')]; EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = :getTemplateId(opp.Product__r.Name, opp.Product__r.Product_Type__c)]; htmlBody = emailTemplate.HtmlValue; subject = emailTemplate.subject; } //Send the email with two attachments public PageReference send() { // Define the email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); //Send email if Branch not equals to Saudi Arabia and Product type is Standard if(opp.Branch_Name__r.Name != 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Standard'){ // Reference the attachment page and pass in the Opportunity ID PageReference pdf1 = Page.CCLPdf;//CCLPdf is a Visualforce page pdf1.getParameters().put('id',opp.id); pdf1.setRedirect(true); // Take the PDF content Blob b1 = pdf1.getContentAsPDF(); PageReference pdf2 = Page.ProformaInvoicePdf;//ProformaInvoicePdf is a Visualforce page pdf2.getParameters().put('id',opp.id); pdf2.setRedirect(true); // Take the PDF content Blob b2 = pdf2.getContentAsPDF(); // Create the email attachment Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment(); efa1.setFileName(opp.account.Name+'-CCL.pdf');//set the email attachment name efa1.setBody(b1); Messaging.EmailFileAttachment efa2 = new Messaging.EmailFileAttachment(); efa2.setFileName(opp.account.Name+'-Invoice.pdf');//set the email attachment name efa2.setBody(b2); // Sets the paramaters of the email email.setSubject(subject);//Auto populate the Subject from Template email.setHtmlBody(htmlBody);//Auto populate the Body from Template //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name) email.setTargetObjectId(opp.Account.PersonContactId); email.setSaveAsActivity(true); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1,efa2}); StoringAttachments(); } //Send email if Branch not equals to Saudi Arabia and Product type is Canada if(opp.Branch_Name__r.Name != 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Canada'){ // Reference the attachment page and pass in the Opportunity ID PageReference pdf3 = Page.CANCCLPdf;//CANCCLPdf is a Visualforce page pdf3.getParameters().put('id',opp.id); pdf3.setRedirect(true); // Take the PDF content Blob b3 = pdf3.getContentAsPDF(); PageReference pdf4 = Page.ProformaInvoicePdf;//ProformaInvoicePdf is a Visualforce page pdf4.getParameters().put('id',opp.id); pdf4.setRedirect(true); // Take the PDF content Blob b4 = pdf4.getContentAsPDF(); // Create the email attachment Messaging.EmailFileAttachment efa3 = new Messaging.EmailFileAttachment(); efa3.setFileName(opp.account.Name+'-CANCCL.pdf');//set the email attachment name efa3.setBody(b3); Messaging.EmailFileAttachment efa4 = new Messaging.EmailFileAttachment(); efa4.setFileName(opp.account.Name+'-CANInvoice.pdf');//set the email attachment name efa4.setBody(b4); // Sets the paramaters of the email email.setSubject(subject);//Auto populate the Subject from Template email.setHtmlBody(htmlBody);//Auto populate the Body from Template //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name) email.setTargetObjectId(opp.Account.PersonContactId); email.setSaveAsActivity(true); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa3,efa4}); StoringAttachments(); } //Send email if Branch is saudi Arabia and Product type is Standard if(opp.Branch_Name__r.Name == 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Standard'){ // Reference the attachment page and pass in the Opportunity ID PageReference pdf5 = Page.KSACCLPdf;//KSACCLPdf is a Visualforce page pdf5.getParameters().put('id',opp.id); pdf5.setRedirect(true); // Take the PDF content Blob b5 = pdf5.getContentAsPDF(); PageReference pdf6 = Page.ProformaInvoicePdf;//ProformaInvoicePdf is a Visualforce page pdf6.getParameters().put('id',opp.id); pdf6.setRedirect(true); // Take the PDF content Blob b6 = pdf6.getContentAsPDF(); // Create the email attachment Messaging.EmailFileAttachment efa5 = new Messaging.EmailFileAttachment(); efa5.setFileName(opp.account.Name+'-KSACCL.pdf');//set the email attachment name efa5.setBody(b5); Messaging.EmailFileAttachment efa6 = new Messaging.EmailFileAttachment(); efa6.setFileName(opp.account.Name+'-KSAInvoice.pdf');//set the email attachment name efa6.setBody(b6); // Sets the paramaters of the email email.setSubject(subject);//Auto populate the Subject from Template email.setHtmlBody(htmlBody);//Auto populate the Body from Template //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name) email.setTargetObjectId(opp.Account.PersonContactId); email.setSaveAsActivity(true); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa5,efa6}); StoringAttachments(); } //Send email if Branch is saudi Arabia and Product type is Canada if(opp.Branch_Name__r.Name == 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Canada'){ // Reference the attachment page and pass in the Opportunity ID PageReference pdf7 = Page.KSACANCCLPdf;//KSACANCCLPdf is a Visualforce page pdf7.getParameters().put('id',opp.id); pdf7.setRedirect(true); // Take the PDF content Blob b7 = pdf7.getContentAsPDF(); PageReference pdf8 = Page.ProformaInvoicePdf;//ProformaInvoicePdf is a Visualforce page pdf8.getParameters().put('id',opp.id); pdf8.setRedirect(true); // Take the PDF content Blob b8 = pdf8.getContentAsPDF(); // Create the email attachment Messaging.EmailFileAttachment efa7 = new Messaging.EmailFileAttachment(); efa7.setFileName(opp.account.Name+'-KSACANCCL.pdf');//set the email attachment name efa7.setBody(b7); Messaging.EmailFileAttachment efa8 = new Messaging.EmailFileAttachment(); efa8.setFileName(opp.account.Name+'-KSACANInvoice.pdf');//set the email attachment name efa8.setBody(b8); // Sets the paramaters of the email email.setSubject(subject);//Auto populate the Subject from Template email.setHtmlBody(htmlBody);//Auto populate the Body from Template //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name) email.setTargetObjectId(opp.Account.PersonContactId); email.setSaveAsActivity(true); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa7,efa8}); StoringAttachments(); } // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); return new PageReference('/'+opp.Id); } //Creating attachments public void StoringAttachments() { //Send attachments if Branch is India and Product type is Standard if((opp.Branch_Name__r.Name == 'India' || opp.Branch_Name__r.Name == 'United Kingdom') && opp.Product__r.Product_Type__c == 'Standard'){ Attachment myAttach1 = new Attachment(); myAttach1.ParentId = opp.Account.PersonContactId; myAttach1.name = opp.account.Name+'-CCL.pdf'; PageReference myPdf1 = Page.CCLPdf; myAttach1.body = myPdf1.getContentAsPdf(); insert myAttach1; Attachment myAttach2 = new Attachment(); myAttach2.ParentId = opp.Account.PersonContactId; myAttach2.name = opp.account.Name+'-Invoice.pdf'; PageReference myPdf2 = Page.ProformaInvoicePdf; myAttach2.body = myPdf2.getContentAsPdf(); insert myAttach2; } //Send attachments if Branch is India and Product type is Canada if((opp.Branch_Name__r.Name == 'India' || opp.Branch_Name__r.Name == 'United Kingdom') && opp.Product__r.Product_Type__c == 'Canada'){ Attachment myAttach3 = new Attachment(); myAttach3.ParentId = opp.Account.PersonContactId; myAttach3.name = opp.account.Name+'-CANCCL.pdf'; PageReference myPdf3 = Page.CANCCLPdf; myAttach3.body = myPdf3.getContentAsPdf(); insert myAttach3; Attachment myAttach4 = new Attachment(); myAttach4.ParentId = opp.Account.PersonContactId; myAttach4.name = opp.account.Name+'-CANInvoice.pdf'; PageReference myPdf4 = Page.ProformaInvoicePdf; myAttach4.body = myPdf4.getContentAsPdf(); insert myAttach4; } //Send attachments if Branch is saudi Arabia and Product type is Standard if(opp.Branch_Name__r.Name == 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Standard'){ Attachment myAttach5 = new Attachment(); myAttach5.ParentId = opp.Account.PersonContactId; myAttach5.name = opp.account.Name+'-KSACCL.pdf'; PageReference myPdf5 = Page.KSACCLPdf; myAttach5.body = myPdf5.getContentAsPdf(); insert myAttach5; Attachment myAttach6 = new Attachment(); myAttach6.ParentId = opp.Account.PersonContactId; myAttach6.name = opp.account.Name+'-KSAInvoice.pdf'; PageReference myPdf6 = Page.ProformaInvoicePdf; myAttach6.body = myPdf6.getContentAsPdf(); insert myAttach6; } //Send attachments if Branch is saudi Arabia and Product type is Canada if(opp.Branch_Name__r.Name == 'Saudi Arabia' && opp.Product__r.Product_Type__c == 'Canada'){ Attachment myAttach7 = new Attachment(); myAttach7.ParentId = opp.Account.PersonContactId; myAttach7.name = opp.account.Name+'-KSACANCCL.pdf'; PageReference myPdf7 = Page.KSACANCCLPdf; myAttach7.body = myPdf7.getContentAsPdf(); insert myAttach7; Attachment myAttach8 = new Attachment(); myAttach8.ParentId = opp.Account.PersonContactId; myAttach8.name = opp.account.Name+'-KSACANInvoice.pdf'; PageReference myPdf8 = Page.ProformaInvoicePdf; myAttach8.body = myPdf8.getContentAsPdf(); insert myAttach8; } } //Reads the Template from Custom settings public String getTemplateId(String prodName, String prodType){ template = [Select Name, Email_Template_Id__c, Product_Type__c from Product_EmailTemplates__c where Name = : prodName and Product_Type__c = : prodType]; return template.Email_Template_Id__c; } }
Can anyone please help me to write a Test class for my Extensions?
Thanks In Advance.......
Ranjith
In your instance you will need to make part of your setup data an instance variable of you SendEmail class, and you will need to pass in a standard controller for the opportunity. [4] I would also highly recommend that you create some utility methods to create your attachments since you have a lot of duplicate code. This will make your testing easier to in the log run.
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://www.salesforce.com/docs/developer/pages/Content/pages_controller_error_handling.htm
For the test class basics go for this post
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Thanks for reply,
Actually I wrote a Test class for it , but it covers only 8%,
I will paste my Code below, and give your valuable inputs to me.
Test Class:
I am getting the error in this line:
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
This is my Error:
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []
Here, to,cc,bcc are optional, if I am not taking those also it should work
Can you please look into it and let me know what's wrong in my code?
Thanks in advance....
Ranjith
setTargetObjectId must be ID of the contact, lead, or user to which the email will be sent.
Check in your code is that ID coming correctly or not & also check is email ID mentioned or not in test data.
If this answers your question then hit Like and mark it as solution!