0

I'm very new to writing test classes for visualforce pages. I currently have a visualforce page that renders as a pdf but uses a custom controller as an extension. My question is really how do I know what I have to test in my controller? The visualforce page also uses a standard controller with a custom object. The custom controller is just grabbing the record id of page and assigning it to a temp object so I can could do a few logical things with the fields so that the pdf displays certain things based on what the field values are.

Here's the custom controller....

public class WH_381_pdf_control {

    /* Instance Variables */
    //1st picklist (reason)
    public String reason1 {get;set;}
    public String reason2 {get;set;}
    public String reason3 {get;set;}
    public String reason4 {get;set;}
    public String reason5 {get;set;}
         //sub-options
    public String r3spouse {get;set;}
    public String r3child {get;set;}
    public String r3parent {get;set;}
    public String r4spouse {get;set;}
    public String r4child {get;set;}
    public String r4parent {get;set;}
    public String r5spouse {get;set;}
    public String r5child {get;set;}
    public String r5parent {get;set;}
    public String r5kin {get;set;}
    //2nd picklist
    public String elig {get;set;}
    public String notelig {get;set;}
    public String noteligA {get;set;}
    public String noteligAmonths {get;set;}
    public String noteligB {get;set;}
    public String noteligC {get;set;}
    //dates
    public date todays {get;set;}
    public String todayDate {get;set;}
    public String beginDate {get;set;}
    public String requestDate {get;set;}
    public String docReturnDate {get;set;}
    //3rd picklist
    public String efRequired {get;set;}
    public String relateDocReq {get;set;}
    public String otherInfoNeed {get;set;}
    public String noAddtInfoNeed {get;set;}
    public String theIS {get;set;}


    public FMLA_WC__c tempFMLA {get;set;}
    public String keyEmploy {get;set;}
    public String keyNOT {get;set;}
    public String periodicReports {get;set;}






    /* Controller */
    public WH_381_pdf_control(ApexPages.StandardController controller) {
        //get record id of this record for the temp record
        this.tempFMLA = (FMLA_WC__c)controller.getRecord();
        //query for fields if not null
        try{
            this.tempFMLA = [SELECT name, id, Reason_for_FMLA__c, Specified_Person__c, Eligible_for_FMLA__c, FMLA_Request_Date__c, FMLA_Request_Begin_Date__c, Documentation_Return_Date__c,
                             Months_of_Service__c, X380E_380F_Required__c, Relationship_Documentation_Required__c, Other_Information_Needed__c, No_Additional_Information_Needed__c, Key_Employee__c,
                             Periodic_Reports_Required__c
                             FROM FMLA_WC__c
                             WHERE FMLA_WC__c.id =: tempFMLA.id];
            //-----------------------1st page of document----------------------------
            //format dates
            todays=System.today();
            todayDate = this.ConvertDateFormat(todays);
            beginDate = this.ConvertDateFormat(tempFMLA.FMLA_Request_Begin_Date__c);
            requestDate = this.ConvertDateFormat(tempFMLA.FMLA_Request_Date__c);
            docReturnDate = this.ConvertDateFormat(tempFMLA.Documentation_Return_Date__c);


            //**1st picklist**
            //1st option
            if (tempFMLA.Reason_for_FMLA__c == 'Birth of child, or placement of child for adoption/foster-care')
                reason1 = 'X';
            //2nd option
            if (tempFMLA.Reason_for_FMLA__c == 'Own serious health conditions')
                reason2 = 'X';
            //3rd option
            if (tempFMLA.Reason_for_FMLA__c == 'Needed to care for... [Specified Person]') {
                reason3 = 'X';
                //sub-options
                if (tempFMLA.Specified_Person__c == 'Spouse')
                    r3spouse = 'X';
                if (tempFMLA.Specified_Person__c == 'Child')
                    r3child = 'X';
                if (tempFMLA.Specified_Person__c == 'Parent')
                    r3parent = 'X';
            }
            //4th option
            if (tempFMLA.Reason_for_FMLA__c == 'Qualifying exigency because your... [Specified Person] is on covered') {
                reason4 = 'X';
                 //sub-options
                if (tempFMLA.Specified_Person__c == 'Spouse')
                    r4spouse = 'X';
                if (tempFMLA.Specified_Person__c == 'Child')
                    r4child = 'X';
                if (tempFMLA.Specified_Person__c == 'Parent')
                    r4parent = 'X';
            }
            //5th option
            if (tempFMLA.Reason_for_FMLA__c == 'You are... [Specified Person] of covered servicemember w injury/illness') {
                reason5 = 'X';
                //sub-options
                if (tempFMLA.Specified_Person__c == 'Spouse')
                    r5spouse = 'X';
                if (tempFMLA.Specified_Person__c == 'Child')
                    r5child = 'X';
                if (tempFMLA.Specified_Person__c == 'Parent')
                    r5parent = 'X';
                if (tempFMLA.Specified_Person__c == 'Next of kin')
                    r5kin = 'X';
            }
            //**2nd picklist**
            if (tempFMLA.Eligible_for_FMLA__c == 'Yes')
                elig = 'X';
            if (tempFMLA.Eligible_for_FMLA__c.startsWith('No')) {
                notelig = 'X';
                //sub-options
                if (tempFMLA.Eligible_for_FMLA__c == 'No -- Have not met 12-month length of service'){
                    noteligA = 'X';
                    //number of months worked so far
                    noteligAmonths = tempFMLA.Months_of_Service__c;
                }
                if (tempFMLA.Eligible_for_FMLA__c == 'No -- Have not met hours of service')
                    noteligB = 'X';
                if (tempFMLA.Eligible_for_FMLA__c == '  No -- Don\'t work and/or report to site with 50+ employees within 75 mi')
                    noteligC = 'X';
            }
            //**3rd picklist**
            if (tempFMLA.X380E_380F_Required__c == true) {
                efRequired = 'X';
                theIS = 'X';
            }
            if (tempFMLA.Relationship_Documentation_Required__c == true)
                relateDocReq = 'X';
            if (tempFMLA.Other_Information_Needed__c == true)
                otherInfoNeed = 'X';
            if (tempFMLA.No_Additional_Information_Needed__c == true)
                noAddtInfoNeed = 'X';

            //-----------------------2nd page of document----------------------------
            //key employee checked
            if (tempFMLA.Key_Employee__c == true){
                keyEmploy = 'X';
                keyNOT = 'X';
            }
            //periodic reports (need checkbox field?)
            periodicReports = String.valueof(tempFMLA.Periodic_Reports_Required__c);


        }
        catch (Exception e) {

        }
    }

    //method for formatting date fields
    public String ConvertDateFormat(Date d){
        String dateString;
        String month;
        if (d!=null){
            integer d1 = d.day();
            integer d2 = d.month();
            if (d2==1)
                month='January';
            if (d2==2)
                month ='February';
            if (d2==3)
                month ='March';
            if (d2==4)
                month ='April';
            if (d2==5)
                month ='May';
            if (d2==6)
                month ='June';
            if (d2==7)
                month ='July';
            if (d2==8)
                month ='August';
            if (d2==9)
                month ='September';
            if (d2==10)
                month ='October';
            if (d2==11)
                month ='November';
            if (d2==12)
                month ='February';
            integer d3 = d.year();
            dateString = month + ' ' +string.valueof(d1) + ', ' + string.valueof(d3);
        }
        return dateString;
    }



}//END WH_381_pdf
1
  • The page is just recreation of a pdf form in html. I am assigning the various strings "X" to basically "check off" certain form fields. So, the "X's" are basically checkmarks on the page. It works and looks great, I just don't know what to test. – Z_wizard18 May 2 '14 at 20:53
1

You basically need to test the extension, that's all. Start by inserting a new record, then construct a new standard controller using that record as the parameter, and finally construct a new instance of your extension and test any methods. Since you have if statements, you may need to test multiple records.

2
  • OK thanks I'll try that... just another general question... when you are in the developer console, and you click on the "Tests" tab in the bottom menu... under the "Overall Code Coverage" section... does the number of lines next to a class mean the number of lines that need to be tested? Like for this controller class, there are 197 lines, but in that section its says "0/2" under the "Lines" column. I'm assuming that means only 2 lines need to be actually tested? – Z_wizard18 May 2 '14 at 20:59
  • That may be an incorrect value. You have to run at least one test to be accurate. When the value is non-zero, the number on the right should indicate the total testable lines. – sfdcfox May 2 '14 at 21:16

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.