You need to sign in to do that
Don't have an account?
Apex developer 21
Help writing a unittest for send email testclass
How do i write a unittest for the email class. I attached my code below:
public class SendemailController { public String OpportunityId {get;set;} Public SendemailController(){ OpportunityId = ApexPages.currentPage().getParameters().get('Id'); } Public Pagereference sendEmailFunction(){ Opportunity getEmail = [SELECT Id, Contact__r.email FROM Opportunity WHERE id=:OpportunityId]; if(getEmail.Contact__r.email != null) { String toaddress = getEmail.Contact__r.email; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {toaddress}; String[] ccAddresses = new String[] {toaddress}; mail.setToAddresses(toAddresses); mail.setCcAddresses(ccAddresses); mail.setReplyTo(toaddress); mail.setSenderDisplayName('Name'); mail.setSubject('Testing email through apex'); mail.setBccSender(false); mail.setUseSignature(true); mail.setPlainTextBody('Dear tester, here are the attechments. This mail is sent trough apex'); List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :OpportunityId]){ Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); } mail.setFileAttachments(fileAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } PageReference reference = new PageReference('https://eu11.salesforce.com/'+ OpportunityId); reference.setRedirect(true); return reference; } }
Please follow the code:
If you get help with this please give me best answer.
Thanks,
All Answers
Please follow the code:
If you get help with this please give me best answer.
Thanks,
Nice work!!! i tested it one small piece,line 28 needs to be: SendemailController Controller = new SendemailController();