I have a Visualforce which has a custom controller with the following code (snippet). As you will see it takes the value from 3 text boxes on the page, does some brief validation before opening a new page:
Page 1 Controller
public PageReference openSheet(){
if (!strday.isNumeric()){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Pleaseenter a correct day value.'));
return null;
}
if (!strmonth.isNumeric()){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter a correct month value.'));
return null;
}
if (!stryear.isNumeric()){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter a correct year value.'));
return null;
}
PageReference newPage = page.crew_sheet;
strday.leftPad(2,'0');
strmonth.leftPad(2,'0');
newPage.getParameters().put('day', strday);
newPage.getParameters().put('month', strmonth);
newPage.getParameters().put('year', stryear);
newPage.setRedirect(true);
return newPage;
}
The Visualforce page that opens also has a custom controller and it uses the following code (snippet):
Page 2 Controller
public with sharing class CrewSheet {
Public String day;
Public String month;
Public String year;
Public Datetime specificDate;
functionalY fy = new functionalYUtilities ();
Public List<Object__c> listAlpha1{get;set;}
Set<String> gwRounds = new Set<String>();
Public List<WorkOrder> listAlpha1Results{get;set;}
Public List<Object__c> listAlpha2{get;set;}
Set<String> pcRounds = new Set<String>();
Public List<WorkOrder> listAlpha2Results{get;set;}
Public List<Object__c> listAlpha3{get;set;}
Set<String> gbRounds = new Set<String>();
Public List<WorkOrder> listAlpha3Results{get;set;}
Public List<Object__c> listAlpha4{get;set;}
Set<String> rRounds = new Set<String>();
Public List<WorkOrder> listAlpha4Results{get;set;}
Public CrewSheet(){
//get the parameters from the page
day = apexpages.currentPage().getParameters().get('day');
month = apexpages.currentPage().getParameters().get('month');
year = apexpages.currentPage().getParameters().get('year');
//the following if statements were added in as the test class isn't passing the parameters
Datetime missingdate = system.now();
if(day == ''){
day = string.valueof(missingdate.day());
}
if(month == ''){
month = string.valueof(missingdate.month());
}
if(year == ''){
year = string.valueof(missingdate.year());
}
String specificDate = day + '/' + month + '/' + year + ' 09:00';
Datetime specificDateformatted = datetime.parse(specificDate);
String nextDayText = specificDateformatted.format('EEEE');
// At this point we use the dates/strings in queries ultimately displayed on the vf page...i've cut them out for make it easier to read.
}
}
I have been trying to write a test class for this, whilst I am a beginner I was able to find a lot of examples on line and I came up with the following. Unfortunately it doesn't work, the apex log always reports "invalid date time null/null/null 09:00".
I did expect this as the parameters are obviously converted to datetime from strings etc but I thought passing the parameters would work. My test sample
Test Class
@isTest
public class testCrewSheet {
@isTest static void testCrewSheet(){
String day;
String month;
String year;
//previous test attempt - same end result:
//Test.setCurrentPageReference(new PageReference('Page.crew_sheet'));
//apexPages.currentPage().getParameters().put('25', day);
//apexPages.currentPage().getParameters().put('04', month);
//apexPages.currentPage().getParameters().put('2017', year);
PageReference pageRef = Page.Crew_Sheet;
pageRef.getParameters().put('25',day);
pageRef.getParameters().put('04',month);
pageRef.getParameters().put('2017',year);
CrewSheet cs = new CrewSheet();
test.setCurrentPageReference(pageRef);
}
}
So to summarise, why aren't the parameters being passed in the test apex class?
Thanks – I am currently only achieving 21% of my code coverage so I havea bit of a way to go 🙁
Best Answer
There are some issues with your testclass
1) You have to assign day/month/year to numbers, not the other way around
2) You have to set the pagereference and then put the params