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
D-VacD-Vac 

Help with test coverage on visualforce component controller

Hi I have a visualforce component with a controller I am trying to write test coverage on.  I had difficulty finding examples on testing the controller of a visualforce component.  One suggestion I read said to create a visualforce page with the controller simply to test it.  So that's what I've done but I could use some help.

Specifically, the controller and the page don't refer to a specific object / record.  They work if you go to the page itself and it queries the database for an aggregate result of opportunity information. 

I need help with the test coverage which is very flimsy and is getting the following error:
Class.Camp_Inf.CampDaysController_MP.Summary.<init>: line 31, column 1
Class.Camp_Inf.CampDaysController_MP.<init>: line 19, column 1
Class.Camp_Inf.testCampInfPages.Test2CampDaysController: line 80, column 1

Here is my controller:

public class CampDaysController_MP {

public Summary[] Summaries { get; set; }

public CampDaysController_MP(){

AggregateResult[] results =
                        [SELECT AVG(Camp_Inf__Days_from_Campaign_Start_to_Opp_Create__c) DayAvg
                         FROM Opportunity
                         WHERE CampaignId != null
                         AND CreatedDate = LAST_N_DAYS:365
                         //ORDER BY CreatedDate
                         //LIMIT 45000
                         ];

Summaries = New List<Summary>();

for (AggregateResult ar :results){
     Summaries.add(new Summary(ar));
     }
     }

                        
public class Summary{

public integer CampToOppDays {get; private set;}
public integer RecommendDays {get; private set;}

public Summary(AggregateResult ar){
Decimal CD = (Decimal) ar.get('DayAvg');
CampToOppDays = CD.intvalue();
Decimal RD = ((CD * 0.33)+ CD);
RecommendDays = RD.intvalue();
}

}
}

Here is the visualforce test page:

<apex:page controller="CampDaysController_MP" >
        <BR/>
        <apex:repeat value="{!Summaries}" var="s" > 
        <apex:outputtext value="Your company's average number of days from Campaign Start Date to Opportunity Create Date (for opportunities created in the last 365 days) is:"/>
        <BR/>
        <apex:outputtext value="{!s.CampToOppDays} days." style="font-size:Medium; color:red"/>
        <BR/>
        <BR/>          
        <apex:outputtext value="Therefore it is recommended you set the window at least at:"/>
        <BR/>
        <apex:outputtext value="{!s.RecommendDays} days." style="font-size:Medium; color:red"/>
        <BR/>
        </apex:repeat>    
</apex:page>


Here is my flimsy test coverage that doesn't work.  The main issue I think I am having is that I'm not able to do a ApexPages.currentPage().getParameters(); on a specific record like I'm used to doing.:

static testMethod void Test2CampDaysController() {

//this is a test utility I have to create some records
     Camp_Inf.TestCampInfUtils_MP t = new Camp_Inf.TestCampInfUtils_MP();
     t.StuffNoSettings(true);  

    
      Test.setCurrentPage(Page.Camp_Inf__CampToOppDaysComponentTester_MP);
  
Test.startTest();
   
      ApexPages.currentPage().getParameters();

      Camp_Inf.CampDaysController_MP idc2 = new Camp_Inf.CampDaysController_MP();

      
Test.stopTest();
}
//End of Test 2

Any help is much appreciated!  Thank you!
Best Answer chosen by D-Vac
D-VacD-Vac
It turns out it was a mistake with the utility class.  the test records didn't have sufficient information to return any results in the query of the controller.  Duh.