1

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.

3 Answers 3

2

You don't need to instatiate a test class. Its just a public class with static methods. Each method generally should run independent of the others. You just need to set up an instance of your controller and set the test page to your VF page, and then you can run the controller methods and assert the returned page is what you are looking for. Something like this should work.

Disclaimer: I did this in a text editor and it hasn't been compiled. May need to tweak, but the general idea is good and should get you headed in the right direction

@isTest
public class linkToVFP_TEST {

    public static testMethod void testMyClass(){
        Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
        insert a;

        PageReference pg = Page.Your_VF_Page_Name;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        linkToVFP customController = new linkToVFP(stdController);

        system.assertEquals(false, a.Joe_Test__c);
        customController.Account.Joe_Test__c = true;
        PageReference newPage = customController.saveAndCongrat();

        Account newAccount = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
        system.assertEquals(true, a.Joe_Test__c);

        system.assertEquals(Page.Congratulations, newPage);
    }
}
4
  • Also, I would separate the methods as a best practice. One method to check the save occurred, and another method to check the PageReference that is returned is as expected. Commented Jul 27, 2014 at 13:10
  • Thank you for helping with the code sfdc_ninja I have changed the code but it is throwing a couple of errors. ** Invalid field initializer: Account.Joe_Test__c ** and ** Variable does not exist: controller.Account.Joe_Test__c ** -- I tried to change it to "Account.Joe_Test__c" in line 5 but it trew some more errors. Commented Jul 27, 2014 at 22:47
  • I have tried it in the browser (not the Developer Console) and getting this error Error: Compile Error: Variable does not exist: controller.Account.Joe_Test__c at line 15 column 9 Commented Jul 29, 2014 at 12:53
  • I used the wrong controller variable name. Should be customController. As I mentioned in the disclaimer, this was done in a text editor, so there might be some syntax issues. Commented Jul 29, 2014 at 20:06
0

Test classes should be private and have static methods that also specify testMethod:

@isTest
private class MyTest
{
    static testMethod void myUnitTest()
    {
        // TO DO: implement unit test
    }
}

Once you have your basic test method set up, it looks like you are correctly setting some page parameters. After this you need to create an instance of linkToVFP and call methods in it.

Account acc = new Account();
linkToVFP ext = new linkToVFP(new ApexPages.StandardController(acc));
ext.saveAndCongrat();

It looks like instead of creating an instance of the class you want to create you are creating an instance of your test class and calling a duplicate of the method you created there:

//This is wrong.  Don't do this.
linkToVFP_TEST testPRTC = new linkToVFP_TEST();
PageReference saveAndCongrat = Page.Congratulations
1
  • Thank you for your help dphil. this is my new code ---------------- I am now getting this Error: Error: Compile Error: Variable does not exist: Account at line 19 column 9. Commented Jul 29, 2014 at 23:04
-1

Got it working - thank you everyone for your help!!

@isTest
public class linkToVFP_TEST {

public static testMethod void testMyClass(){
    Account a = new Account(Name = 'Test Account', Joe_Test__c = false);
    insert a;

    PageReference pg = Page.Congratulations;
    Test.setCurrentPage(pg);

    ApexPages.StandardController stdController = new ApexPages.StandardController(a);
    linkToVFP customController = new linkToVFP(stdController);

    system.assertEquals(false, a.Joe_Test__c);

    a.Joe_Test__c = true;
    PageReference newPage = customController.saveAndCongrat();

    Account results = [Select Id, Joe_Test__c From Account Where Id =: a.Id];
    system.assertEquals(true, results.Joe_Test__c);

   System.assertEquals(Page.Congratulations.getUrl(),newPage.getUrl());
   //system.assertEquals(Page.Congratulations, newPage);
}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .