function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike Young 12Mike Young 12 

Need help writing a test for a VF page with a class extension.

Being new to the Apex code world, I have managed, with some help from several forums, to get the following Class and VF page working.  However, I am totally lost on how to write a test for it.  The page allows the user to address an email and include user selected Attachments from Notes and Attachments from a custom object, Sales_Order__c.  How do I test this with the class, etc.?  Can someone supply some code to get me going in the right direction?
 
VF Page
 
<apex:page standardController="Sales_Order__c" extensions="email_class" showHeader = "false" sidebar = "false">
    <apex:form >
        <apex:pageBlock title="Addressing / Message">
        <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
        <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/> 
        <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>           
        <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
        
            <apex:pageBlock title="Check to include attachment with lease approval. LQR is Required!">
                <apex:pageBlockTable value="{!Sales_Order__c.Attachments}" var="Att">
                    <apex:column headerValue="Check to Include">
                    <apex:inputCheckbox value="{!Sales_Order__c.Review_SC_Complete__c}"/>
                    </apex:column>
                    <apex:column value="{!Att.name}"/>
                    <apex:column value="{!Att.contenttype}"/>
                    <apex:column value="{!Att.createddate}"/>
               </apex:pageBlockTable><p/>
      </apex:pageblock>
      <apex:commandButton value="Send Email" action="{!send}"/>
      <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlock>
    </apex:form>   
</apex:page>
 
Apex Class
 
public class email_class{
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string SOid {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
  
    public email_class(ApexPages.StandardController controller) {
        SOid = ApexPages.currentPage().getParameters().get('id');
    }
   
    Public PageReference send(){
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
        mail.setToAddresses(new String[] {emailTo});
        mail.setCcAddresses(new String[] {emailCC});   //set the cc address
        mail.setSubject(subject);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody(email_body);
        mail.setWhatId(SOid);// Set email file attachments
 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
 
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :SOid]){  // Add to attachment file list 
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); 
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);// Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
    }
}
   
pconpcon
Take a look over this [1] on how to test the controller.  You will need to create your attachment files before calling your send method.  Then you would also want to look at the email methods in the Limits class [2].

[1] https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
[2] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm#apex_System_Limits_getEmailInvocations
SRKSRK
Please try this code

@IsTest
public class TestEdit_Hologram_Label_Extension
{
    static testMethod void TestMethodEdit_Hologram_Label_Extension()
    {


        Sales_Order__c SalesOrderObj = new Sales_Order__c();
        SalesOrderObj.name = 'test record';
        .
        .
        .
        .
        .// insert the erquired field for Sales Order record
        insert SalesOrderObj;
        Attachment att = new Attachment();
        att.name = 'test att';
        att.body = blob.valueof('hello testing');
        att.ParentId =SalesOrderObj.id;
        insert att;
        
         test.startTest();
            PageReference pageRef = Page.<You VF page name with this class>; 
            ApexPages.currentPage().getParameters().put('id',SalesOrderObj.id);
            ApexPages.StandardController stdLead = new ApexPages.StandardController(SalesOrderObj);
            
            email_class ec = new email_class(stdLead);
            
            ec.emailTo  ='test@gmail.com';
            ec.emailCC = 'testcc@gmail.com';
            ec.subject = 'test subject';
            ec.email_body = 'test bidy';
            ec.SOid = SalesOrderObj.id;
            ec.send();
        test.stopTest();
    }
}

 
Mike Young 12Mike Young 12
Pcon and SRK,
Your responses were just what I needed to get this tested and deployed.  Thank you very much.