+ Start a Discussion
Amy ThroppAmy Thropp 

Need help with test coverage for controller extension.

Hi all,

I have created the controller extension shown below, to add a visualforce page to my standard layout for a custom object.

Below is my controller, test class, and visualforce page. Right now, coverage is only 67%.

When I run the visualforce page that uses this extension, it works. How do I interrogate the value of “activePrograms” so I can bump up my coverage. Everything I’ve tried returns nulls.


public class DisplayActivePrograms {
               private final Call_Report__c callReport;
    
    public DisplayActivePrograms(ApexPages.StandardController stdController) {
        this.callReport = (Call_Report__c)stdController.getRecord();
    }

                public list<active_program__c> activePrograms;   
    public list<active_program__c>  getActivePrograms() {
        if (activePrograms == null) {
            activeprograms = [select
                                name,
                                category__r.name,
                                category__r.Category_Description__c,
                                start_date__c,
                                end_date__c
                 from active_program__c
                 where
                                end_date__c >= today];  
           
        }
       
        return activePrograms;
    }


@istest
public class DisplayActivePrograms_test {
  PUBLIC static @istest void DisplayActivePrograms_test() {
     account acct = new account(name='abc', phone = '999999999');
     insert acct;
     Call_report__c cr = new Call_report__c(call_date__c = date.newInstance(2014, 7, 1), customer__c=acct.id);
     insert cr;
     srp2__c cat = new srp2__c(name='123',category_description__c = '123 description');
     insert cat;
     date sdt = date.newInstance(2014, 6, 23);
     date edt = date.newInstance(2014, 8, 31);
     active_program__c ap = new active_program__c(name='test name for category 462', category__c=cat.id,start_date__c = sdt, end_date__c = edt);
     insert ap;
   
     ApexPages.StandardController sc = new ApexPages.standardController(cr);
     system.assert(sc != null);

  }
}



<apex:page standardcontroller="Call_Report__c" extensions="DisplayActivePrograms">
   <apex:pageBlock Title="Active Programs">
    <table width="100%">
        <tr>
        <td> <b>Program Name</b> </td>
        <td> &nbsp; </td>
        <td> <b>Category</b> </td>
        <td> &nbsp; </td>
        <td> <b>Category Name</b> </td>
        <td> &nbsp; </td>
        <td> <b>Start Date</b> </td>
        <td> &nbsp; </td>
        <td> <b>End Date</b> </td>
        </tr>
        <tr>
        <td colspan="9"> <hr/> </td>
        </tr>
    <apex:repeat value="{!activePrograms}" var="a">
       
        <tr>
            <td> {!a.Name} </td>
                    <td> &nbsp; </td>
            <td> {!a.Category__r.name} </td>
                    <td> &nbsp; </td>
            <td> {!a.Category__r.Category_Description__c} </td>
                    <td> &nbsp; </td>
            <td><apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                <apex:param value="{!a.Start_Date__c}" />
                </apex:outputText> </td>
                        <td> &nbsp; </td>
            <td><apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                <apex:param value="{!a.End_Date__c}" />
                </apex:outputText> </td>
           
        </tr>
    </apex:repeat>
        </table>

    </apex:pageBlock>
</apex:page>
Best Answer chosen by Amy Thropp
Vishant ShahVishant Shah
Hi

Please look here, you're missing to set the page reference in your test class

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

Ta
Vish

All Answers

Vishant ShahVishant Shah
Hi

Please look here, you're missing to set the page reference in your test class

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

Ta
Vish

This was selected as the best answer
Amy ThroppAmy Thropp
Thanks Vish. Did the trick.

Here's what ended up working. Thought others who struggle with test classes would appreciate.


@isTest
                   
public class DisplayActivePrograms_test{

    public static testMethod void DisplayActivePrograms_test() {
       
    pagereference pageref = page.callReport;
    test.setcurrentpage(pageref)    ;
   
account acct = new account(name='abc', phone = '999999999');
    insert acct;
    Call_report__c cr = new Call_report__c(call_date__c = date.newInstance(2014, 7, 1), customer__c=acct.id);
    insert cr;
    srp2__c cat = new srp2__c(name='123',category_description__c = '123 description');
    insert cat;
    date sdt = date.newInstance(2014, 6, 23);
    date edt = date.newInstance(2014, 8, 31);
    active_program__c ap = new active_program__c(name='test name for category 462', category__c=cat.id,start_date__c = sdt, end_date__c = edt);
    insert ap;
   
    
     apexpages.standardcontroller sc = new apexpages.standardcontroller(cr);
     displayActivePrograms e = new displayActivePrograms(sc);
    
     system.assert(sc != null);
     list <active_program__c> ap2 = e.getActivePrograms();
     system.assert(ap2[0].name == 'test name for category 462');
     system.assert(ap2[0].category__r.Category_Description__c  == '123 description');
     system.assert(ap2[0].category__r.name == '123');
     system.assert(ap2[0].start_date__c== sdt);
     system.assert(ap2[0].end_date__c == edt);
                  
    }
}

Vishant ShahVishant Shah
Awesome!! Please mark this as best answer so someone else can benefit from it. 

Ta,
Vish
Jonathan Meltzer 14Jonathan Meltzer 14
This turned out to be very helpful, indeed.