3

I need a test class for sending a email

public blob body;   
        public PageReference pdfmethod(){
        try
        {
         // to get mail id
         Opportunity_Potential__c opp= [Select id,Account__r.Name,Account__r.Con_Email__pc,Account__r.Con_Email1__pc,Account__r.Con_Email2__pc from Opportunity_Potential__c where id=:qp];
         System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+opp);
         Set<id> ac= new Set<id>();

         String emailid=opp.Account__r.Con_Email__pc;

         String email1id=opp.Account__r.Con_Email1__pc;
         PageReference ref=page.Rent_Resell;
         body=ref.getContentAsPDF();

         Messaging.EmailFileAttachment mefa=new Messaging.EmailFileAttachment();
         mefa.setContentType('application/pdf');
         mefa.setFileName('Property Recommendation');
         mefa.Body=body;

         Messaging.SingleEmailMessage msem=new Messaging.SingleEmailMessage();
         msem.setToAddresses(new String[] {+emailid});
         msem.setSubject('VINTAGE PROPERTY RECOMMENDATION');
         msem.setHtmlBody('<p><font><span style="color:#535664"><b>Dear Sir/Madam,</b></span></font></p><p><font><span style="color:#535664">Greetings from <b>Vintage!</b></span></font></p> <p><font><span style="color:#535664">This is further to your enquiry for Property Search services with our company. Please find alongwith some of the property options that is Available with us & which may suit your requirement. Request you to review the same and get back to us for any clarification or assistance.</span></font></p><p><font><span style="color:#535664">It would be our endeavor to help you get a suitable property at the earliest. We hope you would give us active support to us in order to render Quality & Professional service.</span></font></p>');
         msem.setFileAttachments(new Messaging.EmailFileAttachment[] {mefa});
         //send email
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {msem});

         // Edited code starts here
         if(email1id != null)
         {
         Messaging.EmailFileAttachment mefa1=new Messaging.EmailFileAttachment();
         mefa1.setContentType('application/pdf');
         mefa1.setFileName('Property Recommendation');
         mefa1.Body=body;

         Messaging.SingleEmailMessage msem1=new Messaging.SingleEmailMessage();
         msem1.setToAddresses(new String[] {+email1id});
         msem1.setSubject('VINTAGE PROPERTY RECOMMENDATION');
         msem1.setHtmlBody('<p><font><span style="color:#535664"><b>Dear Sir/Madam,</b></span></font></p><p><font><span style="color:#535664">Greetings from <b>Vintage!</b></span></font></p> <p><font><span style="color:#535664">This is further to your enquiry for Property Search services with our company. Please find alongwith some of the property options that is Available with us & which may suit your requirement. Request you to review the same and get back to us for any clarification or assistance.</span></font></p><p><font><span style="color:#535664">It would be our endeavor to help you get a suitable property at the earliest. We hope you would give us active support to us in order to render Quality & Professional service.</span></font></p>');
         msem1.setFileAttachments(new Messaging.EmailFileAttachment[] {mefa1});
         //send email
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {msem1});

         }

         // Edited code ends here 

        List<Apartments__c> ap=new List<Apartments__c>();
         ap=[select id from Apartments__c where id in:ids];
         System.debug('&&&&&&&&&&&&&&'+ids);

         for(Apartments__c cw: ap){
          Recommendation__c r=new Recommendation__c();
         r.Apartment_Name__c=cw.id;
         //jaba
         r.Opportunity_Potential__c=qp;
         insert r;
       }
       System.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+selectedCategories);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Success!  Email has been sent to customer Email Id:'+emailid , +email1id ));
         }catch(Exception ex){

         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'+ex));
        }

i should get 100% code coverage when i write this in my test class

classname cn=new classname();
cn.pdfmethod();

method is passing but i'm not getting code coverage,

And how to write test class for email with pdf attachment.

3

You can't use

getContentAsPDF

In Test.

The only way is to use Test.isRunningTest():

if (!Test.isRunningTest()){
    body = thePage.getContentAsPDF(); 
}
else{
    body = Blob.valueof('Some random String');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.