+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Email Test Class

Will someone help me with writing a test class for my email class?
 
public with sharing class PDF_EmailController {
    
    @auraEnabled

    public static void SendAttachment(String recordId){
           
        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c FROM Purchase_Order__c
        WHERE Id =: recordId]){
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

            PageReference pref = page.POPDF;
            pref.getParameters().put('Id',por.Id);
            pref.setRedirect(true);
            Blob b = pref.getContent();
            attach.setFileName('POR.pdf');
            attach.setBody(b);
            semail.setSubject('Purchase Order Request Details');
            semail.setToAddresses(new String[] { por.Email_Address__c });
            semail.setPlainTextBody('Please find the attached POR details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});

        }

        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c,Version__c FROM Purchase_Order__c
                                             WHERE Id =: recordId]){
            PageReference PDf =  Page.PORequestPDF;
            PDf.getParameters().put('Id',por.Id);
            PDf.setRedirect(true);
            Attachment attach = new Attachment();
            Blob b ;
            b = PDf.getContent();
            attach.Body = b;
            attach.Name = 'Purchase Order Confirmation ' + System.today();
            attach.IsPrivate = false;
            attach.ParentId = por.Id;
            insert attach;

        }
    }
 }



Visualforce Page

<apex:page controller="PDF_EmailController" standardStylesheets="false" showHeader="false">    
    <center>
        <P style="font-size:22px;"><u>Send Purchase Order Request</u></P>
        <input type="text" value="" id="txtEmailAddress"/><br/><br/>
        <input type="button" onclick="SendEmailAttachment();" value="Send"/>
    </center>
    <script>
    function SendEmailAttachment(){
        var PORId = '{!$CurrentPage.parameters.Id}';
        var emailAddress=document.getElementById('txtEmailAddress').value;
        if(emailAddress!=null && emailAddress!=''){
            TestHandler.SendAttachment(emailAddress,porId,function(result,event){
                if(event.status){
                    if(result=='SUCCESS'){
                        alert('Email sent successfully.');
                    }
                    else{
                        alert(result);
                    }
                }
            })
        }
        else{
            alert('Please provide email address!');
        }
    }
    </script>
 </apex:page>

Thank you,
P
Best Answer chosen by Phuc Nguyen 18
Maharajan CMaharajan C
Hi Phuc,

Follow the below changes in Apex Class also because the getContent() pdf method won't support in testclass. So we have to include the Test.IsRunningTest() check in actual class.
 
public with sharing class PDF_EmailController {
    
    @auraEnabled

    public static void SendAttachment(String recordId){
           
        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c FROM Purchase_Order__c
        WHERE Id =: recordId]){
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

            PageReference pref = page.POPDF;
            pref.getParameters().put('Id',por.Id);
            pref.setRedirect(true);
            Blob b;
            if (Test.IsRunningTest())
            {
                b=Blob.valueOf('Test Class Body');
            }
            else
            {
                b=pref.getContent();
            }
            attach.setFileName('POR.pdf');
            attach.setBody(b);
            semail.setSubject('Purchase Order Request Details');
            semail.setToAddresses(new String[] { por.Email_Address__c });
            semail.setPlainTextBody('Please find the attached POR details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});

        }

        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c,Version__c FROM Purchase_Order__c
                                             WHERE Id =: recordId]){
            PageReference PDf =  Page.PORequestPDF;
            PDf.getParameters().put('Id',por.Id);
            PDf.setRedirect(true);
            Attachment attach = new Attachment();
            Blob b ;
            if (Test.IsRunningTest())
            {
                b=Blob.valueOf('Test Class Body');
            }
            else
            {
                b=PDF.getContent();
            }
            attach.Body = b;
            attach.Name = 'Purchase Order Confirmation ' + System.today();
            attach.IsPrivate = false;
            attach.ParentId = por.Id;
            insert attach;

        }
    }
 }

Test Class:

Add all the required fields below to create the Purchase_Order_Request__c Record also if any parent records needed insert those also.
 
@isTest
public class PDF_EmailControllerTest {
    static testmethod void PDF_EmailControllerTestMethod(){
	// Add all the required fields below to create the Purchase_Order_Request__c Record
        Purchase_Order_Request__c por = new Purchase_Order_Request__c (Name = 'Test name', Email_Address__c = 'test@test.com');
        insert por;
	Test.StartTest();
        PDF_EmailController.SendAttachment(por.Id);
	Test.StopTest();
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Phuc,

Follow the below changes in Apex Class also because the getContent() pdf method won't support in testclass. So we have to include the Test.IsRunningTest() check in actual class.
 
public with sharing class PDF_EmailController {
    
    @auraEnabled

    public static void SendAttachment(String recordId){
           
        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c FROM Purchase_Order__c
        WHERE Id =: recordId]){
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

            PageReference pref = page.POPDF;
            pref.getParameters().put('Id',por.Id);
            pref.setRedirect(true);
            Blob b;
            if (Test.IsRunningTest())
            {
                b=Blob.valueOf('Test Class Body');
            }
            else
            {
                b=pref.getContent();
            }
            attach.setFileName('POR.pdf');
            attach.setBody(b);
            semail.setSubject('Purchase Order Request Details');
            semail.setToAddresses(new String[] { por.Email_Address__c });
            semail.setPlainTextBody('Please find the attached POR details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});

        }

        for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c,Version__c FROM Purchase_Order__c
                                             WHERE Id =: recordId]){
            PageReference PDf =  Page.PORequestPDF;
            PDf.getParameters().put('Id',por.Id);
            PDf.setRedirect(true);
            Attachment attach = new Attachment();
            Blob b ;
            if (Test.IsRunningTest())
            {
                b=Blob.valueOf('Test Class Body');
            }
            else
            {
                b=PDF.getContent();
            }
            attach.Body = b;
            attach.Name = 'Purchase Order Confirmation ' + System.today();
            attach.IsPrivate = false;
            attach.ParentId = por.Id;
            insert attach;

        }
    }
 }

Test Class:

Add all the required fields below to create the Purchase_Order_Request__c Record also if any parent records needed insert those also.
 
@isTest
public class PDF_EmailControllerTest {
    static testmethod void PDF_EmailControllerTestMethod(){
	// Add all the required fields below to create the Purchase_Order_Request__c Record
        Purchase_Order_Request__c por = new Purchase_Order_Request__c (Name = 'Test name', Email_Address__c = 'test@test.com');
        insert por;
	Test.StartTest();
        PDF_EmailController.SendAttachment(por.Id);
	Test.StopTest();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Maharajan C, that was perfect.  I will need to remeber that the getContent() does not work in Tets Classes.  
Cheers,
P