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
kevin.chileskevin.chiles 

Help with Coverage of Home Page Class

Hello and Hold on to your hats folks I got a doosie! 

 

So the long and short is I am trying to implement a custom home page for my customer portal users.  This has 2 buttons, submit a case to support and submitt a case to dev.  I am using this class as a controller but I am not getting any coverage.

 

public class Home_Page_testsfdc {

    public Home_Page_testsfdc() {

    }

public Home_Page_testsfdc(ApexPages.StandardController controller) 
  {
  }
  
  public pagereference redirect1()
 {   
  
   pagereference p=new pagereference('https://cs8.salesforce.com/500/e?retURL=%2Fhome%2Fhome.jsp%3Fsdtd%3D1&RecordType=012C0000000GGL3&ent=Case');
  return p;
}
public pagereference redirect2()
 {   
  
   pagereference p=new pagereference('https://cs8.salesforce.com/500/e?retURL=%2Fhome%2Fhome.jsp%3Fsdtd%3D1&RecordType=012C0000000GGL3&ent=Case');
  return p;
}

}

 

The question then is, do I need to cover this code in the class itself or would I need to create a test class to cover this bad boy?  

 

 

Kev

 

Best Answer chosen by Admin (Salesforce Developers) 
Michael_TorchedloMichael_Torchedlo

It's very similar to a regular apex class.  The VF pages do not need to have code coverage, only the methods of the class, so you directly invoke as though it was a regular global class without any VF page.

Try something like this

 

@isTest
private class TestController{    
    /*this one should test the no-argument constructor */
    public static testMethod void TestCase1(){
        Home_Page_testsfdc x = new Home_Page_testsfdc();
        PageReference y = x.redirect1();
        y = x.redirect2();
    }

    /*this one should test the constructor that uses a standard controller */
    /*I don't know what object type you are expecting for your standard controller, this code tries for Account */
    public static testMethod void TestCase2(){
        Account E = new Account();
        ApexPages.StandardController sc = new ApexPages.standardController(E);
        Home_Page_testsfdc x = new Home_Page_testsfdc(sc);
        PageReference y = x.redirect1();
        y = x.redirect2();

    }
}

 

All Answers

Michael_TorchedloMichael_Torchedlo

Apex controllers do need to have test coverage before they can be deployed, just like all other Apex classes in the Force.com API.  You can create a separate test class, or if you roll back the API version on your class (v27.0 for example)you can create test method inside the controller class.  You can get coverage either way, but if you use features that require v 28 or later, then just create a separate test class. 

 

More on "Testing Custom Controllers and Controller Extensions"

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

kevin.chileskevin.chiles

Thanks!  I have been toying with this for a minute.  How would I write a test class for this though?  I ask because when writing a class for an object, I create records.  But this is a class controlling a visualforce page that hosts the buttons.  Not sure what to do here.  Do you have an example that I could use?

Michael_TorchedloMichael_Torchedlo

It's very similar to a regular apex class.  The VF pages do not need to have code coverage, only the methods of the class, so you directly invoke as though it was a regular global class without any VF page.

Try something like this

 

@isTest
private class TestController{    
    /*this one should test the no-argument constructor */
    public static testMethod void TestCase1(){
        Home_Page_testsfdc x = new Home_Page_testsfdc();
        PageReference y = x.redirect1();
        y = x.redirect2();
    }

    /*this one should test the constructor that uses a standard controller */
    /*I don't know what object type you are expecting for your standard controller, this code tries for Account */
    public static testMethod void TestCase2(){
        Account E = new Account();
        ApexPages.StandardController sc = new ApexPages.standardController(E);
        Home_Page_testsfdc x = new Home_Page_testsfdc(sc);
        PageReference y = x.redirect1();
        y = x.redirect2();

    }
}

 

This was selected as the best answer
kevin.chileskevin.chiles

Thank you!  Sir that works like a charm.  You are a legend good sir!