You need to sign in to do that
Don't have an account?
Write test class for Visual Force pages
Hi,
I have a bunch of VF pages. I have created test class and have added test methods for each page. However I would like to take this further to meet the expected test %.
My VF pages has custom controllers assigned. In the custon controller class there are child classes declared.
What should be added to the below test class to increase test %.
Thanks.
Visual Force Page
public class TripByMonth{
Private List <AggregateResult> LstTripLegs=new List <AggregateResult>();
Private List <AggregateResult> LstTripLegCities=new List <AggregateResult>();
Private List <MonthlyTrip> lstJanTrips=new List <MonthlyTrip>();
Private List <MonthlyTrip> lstFebTrips=new List <MonthlyTrip>();
Private List <MonthlyTripArray> lstAllTrips=new List <MonthlyTripArray>();
//Constructor of the class
PublictTripByMonth(){
LstTripLegs=[SELECT To_City__r.Name TravelTo,Trip__r.Analyst__r.Name Analyst, Calendar_Month(Trip_leg_From_date__c)cal_mon
FROM Leg__C GROUP BY Calendar_Month(Trip_leg_From_date__c), To_City__r.Name,Trip__r.Analyst__r.Name
ORDER BY To_City__r.Name];
LstTripLegCities=[SELECT To_City__r.Name TravelTo, Count(Trip__r.Analyst__r.Name) CntAn,Calendar_Month(Trip_leg_From_date__c)cal_mon
FROM Leg__C GROUP BY Calendar_Month(Trip_leg_From_date__c), To_City__r.Name
ORDER BY To_City__r.Name];
}
//Trip record list
Public List <LegInfo> getLegsForReport(){
List <LegInfo> LstLegs = new List <LegInfo>();
for (AggregateResult ARs:LstTripLegs){
LegInfo ObjLegInfo=new LegInfo(ARs);
LstLegs.Add (ObjLegInfo);
}
Return LstLegs;
}
//Create All Trips array
Public List <MonthlyTripArray> getAllTrips(){
String stOldCity='';
String stJanAnalysts='';
String stFebAnalysts='';
String stMarAnalysts='';
String stAprAnalysts='';
for (AggregateResult ARJan:LstTripLegs){
if(stOldCity ==''){
stOldCity = (String)ARJan.get('TravelTo');
}
if(stOldCity != (String)ARJan.get('TravelTo')){
MonthlyTripArray ObjAllMonthlyTrip=new MonthlyTripArray (stOldCity,StJanAnalysts,StFebAnalysts,StMarAnalysts,StAprAnalysts);
lstAllTrips.Add (ObjAllMonthlyTrip);
stOldCity = (String)ARJan.get('TravelTo');
StJanAnalysts='';
StFebAnalysts='';
StMarAnalysts='';
StAprAnalysts='';
Continue;
}
if ((Integer) ARJan.get('cal_mon') == 1) {
StJanAnalysts = StJanAnalysts + (String)ARJan.get('Analyst')+ '<br>';
}
if ((Integer) ARJan.get('cal_mon') == 2) {
StFebAnalysts = StFebAnalysts + (String)ARJan.get('Analyst')+ '<br>';
}
}
return lstAllTrips;
}
//****************************************************
Class MonthlyTripArray{
Public String CityName{get; set;}
Public String JanAnalysts{get; set;}
Public String FebAnalysts{get; set;}
Public MonthlyTripArray(String Cn, String JAn, String FAn){
CityName = Cn;
JanAnalysts=JAn;
FebAnalysts=FAn;
}
}
//****************************************************
Class MonthlyTrip{
Public String CityName{get; set;}
Public String Analysts{get; set;}
Public MonthlyTrip(String Cn, String An){
CityName = Cn;
Analysts=An;
}
}
}
Test Class method-
Public static testMethod void VFPageTest_TripDashboard(){
PageReference pgRef = Page.TripDashboard;
Test.setCurrentPageReference (pgRef);
TripByMonth myPgCont = new TripByMonth();
}
Hi,
I may be reading this wrong, but you have only invoked the constructor of ur controller class.
You will also need to invoke the other methods in this class to improve code coverage.
Add more statements like myPgCont.getAllTrips() to your testmethod.
Jerun Jose
All Answers
Hi,
I may be reading this wrong, but you have only invoked the constructor of ur controller class.
You will also need to invoke the other methods in this class to improve code coverage.
Add more statements like myPgCont.getAllTrips() to your testmethod.
Jerun Jose
There is a good explanation of VF test code 3/4 of the way down this page: http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
I use it as a reference everytime I write test code.
Hi
here is the test class
@isTest
private class TripByMonth_test
{
private static testMethod void TripByMonth_test()
{
TripByMonth tbm = new TripByMonth();
tbm.getLegsForReport();
tbm.getAllTrips();
TripByMonth .MonthlyTripArray tbm1 =new TripByMonth .MonthlyTripArray();
TripByMonth .MonthlyTrip tbm2 = new TripByMonth .MonthlyTrip();
}
and in the above test class u have to satisfy the if conditions which are in controller Let me know if any issue
if it solves ur problems means make it as solved
SRS
public class add1
{
public integer a{get;set;}
public integer b{get;set;}
public integer ans{get;set;}
public PageReference add(){
ans = a+b;
return null;
}
static testmethod void testForadd1(){
add1 ad = new add1();
ad.a=4;
ad.b=3;
ad.add();
}