+ Start a Discussion

Help with controller extension tests

I have a controller that I wrote to extend the Opportunity standard controller.  Essentially it supports a VF page that gives a list of Opportunities that fit custom criteria and allow you to update some fields on those opportunities.  Like an editable list view.


The code is working great, but I can't seem to get ANY test coverage.  I am still very new at controllers, only modifying them up until this point, so I've culled together some things that I thought should work.


Controller Extension:


public with sharing class myContractController {

List<Opportunity> lstOpportunity;

Integer numDays = integer.valueOf(ApexPages.currentPage().getParameters().get('days'));

Date dtStart = date.today();

Date dtEnd = dtStart.addDays(numDays);

public myContractController(ApexPages.StandardController controller) {

lstOpportunity = (List<Opportunity>)[Select Id, Name, Hold_MR_Follow_up__c, ContractEndDate__c, StageName, Maintenance_Email__c, Special_Handling__c

From Opportunity

where Hold_MR_Follow_up__c = false AND IsClosed = false AND ContractEndDate__c > :dtStart AND ContractEndDate__c < :dtEnd ORDER BY ContractEndDate__c ASC];


public List<Opportunity> getlstOpportunity(){

return lstOpportunity;


public PageReference save() {

update lstOpportunity;

return ApexPages.currentPage();



 VF Page:


<apex:page standardController="Opportunity" extensions="myContractController">

<apex:form >

<apex:sectionHeader title="Opportunities with Contract End Dates within {!$CurrentPage.parameters.days} days"/>

<apex:pageBlock mode="edit">

<apex:pageMessages />

<apex:pageBlockSection title="Opportunities" >

<apex:pageblocktable value="{!lstOpportunity}" var="AC" id="opportunityTable">

<apex:column headerValue="Hold MR Follow Up">

<apex:inputField value="{!AC.Hold_MR_Follow_up__c}" />


<apex:column headerValue="Opportunity">

<apex:outputLink value="/{!AC.Id}" style="white-space:nowrap;" target="_blank">{!AC.Name}</apex:outputLink>


<apex:column value="{!AC.ContractEndDate__c}"></apex:column>

<apex:column headerValue="Stage" style="white-space:nowrap;">

<apex:outputField value="{!AC.StageName}"></apex:outputField>


<apex:column value="{!AC.Special_Handling__c}"></apex:column>

<apex:column value="{!AC.Maintenance_Email__c}"></apex:column>



<apex:pageBlockButtons location="bottom">

<apex:commandButton value="Save" action="{!save}"/>

<apex:commandButton value="Cancel" action="{!cancel}"/>


<apex:pageBlockSection title="Links">

<apex:outputLink value="/apex/MRChecklist?days=67">67 Day Checklist</apex:outputLink><br/><br/>

<apex:outputLink value="/apex/MRChecklist?days=37">37 Day Checklist</apex:outputLink><br/><br/>

<apex:outputLink value="/apex/MRChecklist?days=21">21 Day Checklist</apex:outputLink><br/><br/>

<apex:outputLink value="/apex/MRChecklist?days=14">14 Day Checklist</apex:outputLink><br/><br/>






 And my attempt at a test class:


public class myContractControllerTest {

public static testMethod void testmyContractController() {

Opportunity o = new Opportunity(name='testOpp', closeDate=date.Today(),Hold_MR_Follow_up__c=false, ContractEndDate__c=date.today().addDays(20), StageName='Qualified', Maintenance_Email__c='bplum@tableausoftware.com', Special_Handling__c=false);

insert o;


ApexPages.StandardController oppCtrl = new ApexPages.Standardcontroller(o);

myContractController controller = new myContractController(oppCtrl);

PageReference testPageRef = new PageReference('/apex/MRChecklist');


String nextPage = controller.save().getUrl();

System.assertEquals('/apex/failure?error=noParam', nextPage);

ApexPages.currentPage().getParameters().put('days', '67');


String myPage = controller.save().getUrl();




If anyone can give me some advice, it would be great!





Message Edited by BarryPlum on 09-24-2009 01:55 PM
Message Edited by BarryPlum on 09-24-2009 02:00 PM

Does your test class have the @isTest decorator above the class declaration?  It looks like you created your test class like a default class rather than as a test class.



I'm pretty sure the "testMethod" covers this.  I tried adding @isTest and it said that the option was already set.
You are also not bracketing your tests with Test.startTest() and Test.stopTest()

It seems ok to me, at least, 0% coverage doesn't make sense with it.


How did you test it? Did you really run THE test class?



Well, at first, I was choosing "Run Tests" with the controller selected instead of the test class (PEBKAC).


I've also added the begin and end test tags, though I don't know if it made a difference.


I'm up to 27% now.  Some of the lines that aren't tested are the date assignment variables.  How are you supposed to test that?


I'll post the updated test code, thanks for the ideas, keep 'em coming!


Well, if you use Force.com IDE, try selecting test class and Run Tests. At least in my case, runing test the controller itself does nothing unless you implemented testmethods in the controller class itself (imagine that how IDE could know which class has the testmethod for the class?)


And try to test from GUI (browser) from Apex Classes > Run All Test. It has the line by line coverage check editor.


anyway, good luck. Test coverage is a tough one, but your code seems not that much tough.



Message Edited by ThomasTT on 09-25-2009 01:56 AM
You should be able to test the date assignment stuff in the constructor by moving your Test.setCurrentPageRef so that it is set prior to the instantiation of your controller.
you also need to call the getter for the lstOpportunities explicitly to get that line covered

Thanks for all your help, I'm up to 93% coverage.  I put a default numDays value in the controller in case there is no variable passed... Now it's not covering the normal use case of with the parameter for some reason.


Here is the code that I'm going with to production:



public class myContractControllerTest {

public static testMethod void testmyContractController() {

Opportunity o = new Opportunity(name='testOpp', closeDate=date.today() ,Hold_MR_Follow_up__c=false, ContractEndDate__c=date.today().addDays(20), StageName='Qualified', Maintenance_Email__c='barry@plumlet.org', Special_Handling__c=false);

insert o;

// test page view


PageReference testPageRef = Page.MRChecklist;




PageReference testPageRef2 = Page.MRChecklist;



ApexPages.StandardController oppCtrl = new ApexPages.Standardcontroller(o);

myContractController controller = new myContractController(oppCtrl);









Message Edited by BarryPlum on 09-25-2009 11:09 AM