I have set up a simple APEX class that is an extension for a Visualforce page. The VF page has a commandButton that saves and takes you to a different page called "Congratulations.vfp"
I have been searching and searching and I have set up a test that seems to work with a PageReference but I am getting a 0% code coverage result. I know that I need to instantiate the class linkToVFP in the test but not sure where?
Here is my Code:
APEX CLASS --
public class linkToVFP {
private ApexPages.StandardController controller;
public linkToVFP(ApexPages.StandardController controller) {
this.controller = controller;
}
public PageReference saveAndCongrat() {
controller.save(); // This takes care of the details for you.
PageReference congratsPage = Page.Congratulations;
congratsPage.setRedirect(true);
return congratsPage;
}
}
VF Page --
<apex:page standardController="Account" extensions="linkToVFP">
<apex:form >
<apex:inputCheckbox value="{!Account.Joe_Test__c}" selected="" />
<apex:commandButton value="Go To New Page" action="{!saveAndCongrat}"/>
</apex:form>
</apex:page>
Test --
@isTest
public class linkToVFP_TEST {
PageReference saveAndCongrat(){
PageReference goToSomeOtherPage = Page.Congratulations;
goToSomeOtherPage.setRedirect(false);
goToSomeOtherPage.getParameters().put('a','b');
goToSomeOtherPage.getParameters().put('c','d');
goToSomeOtherPage.getParameters().put('e','f');
return goToSomeOtherPage;
}
public static testMethod void testMyClass(){
linkToVFP_TEST testPRTC = new linkToVFP_TEST();
PageReference saveAndCongrat = Page.Congratulations;
saveAndCongrat.setRedirect(false);
saveAndCongrat.getParameters().put('e','f');
saveAndCongrat.getParameters().put('a','b');
saveAndCongrat.getParameters().put('c','d');
system.assert(arePageReferencesEqual(saveAndCongrat,
testPRTC.saveAndCongrat()));
// system.assertEquals(Page.Congratulations.getURL(), testPRTC.saveAndCongrat().getURL());
}
static Boolean arePageReferencesEqual(PageReference page1,
PageReference page2){
// First do a null test.
if (page1 == null && page2 == null) return true;
if (page1 == null && page2 != null) return false;
if (page1 != null && page2 == null) return false;
// If none of the page references are null, compare their attributes.
if (page1.getAnchor() == page2.getAnchor()
&& page1.getURL() == page2.getURL()
&& page1.getRedirect() == page2.getRedirect()
&& page1.getCookies() == page2.getCookies()
&& page1.getHeaders() == page2.getHeaders()
&& page1.getParameters() == page2.getParameters()){
return true;
}
return false;
}
}
Thank you for your help in advance.