+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

Test Class for Page Redirect with record Type

HI there,

I have a controlller that navigates a user to a specific visualforce page depending on what record type they select.

Does anyone have any examples of what the test class should be?

Thanks

Controller:
public class LeadNewController {

    public LeadNewController(ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference getRedir() {

        PageReference newPage;

        if (ApexPages.currentPage().getParameters().get('RecordType') == '01230000000WIKN') {
            newPage = Page.PartnershipsLead;
            return newPage.setRedirect(true);
        } 
       else if (ApexPages.currentPage().getParameters().get('RecordType') == '01230000000WIKS') {
            newPage = Page.CorporateSalesLead;
            return newPage.setRedirect(true);
        } 
         else{
            return null;
        }

    }

    private final ApexPages.StandardController controller;
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes. I hope that will help you
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

please try below code.
@isTest 
public class ExtensionTestClass 
{
 static testMethod void testMethod1() 
 {
	 Account testAccount = new Account();
	 testAccount.Name='Test Account' ;
	 insert testAccount;

	 Test.StartTest(); 
	  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
	  LeadNewController  testAccPlan = new LeadNewController (sc);

	  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
	  pageRef.getParameters().put('RecordType', '01230000000WIKN');
	  Test.setCurrentPage(pageRef);
	  testAccPlan.getRedir();
	 Test.StopTest();
 }
 
 static testMethod void testMethod2() 
 {
	 Account testAccount = new Account();
	 testAccount.Name='Test Account' ;
	 insert testAccount;

	 Test.StartTest(); 
	  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
	  LeadNewController  testAccPlan = new LeadNewController (sc);

	  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
	  pageRef.getParameters().put('RecordType', '01230000000WIKS');
	  Test.setCurrentPage(pageRef);
	  testAccPlan.getRedir();
	 Test.StopTest();
 }
 
 static testMethod void testMethod3() 
 {
	 Account testAccount = new Account();
	 testAccount.Name='Test Account' ;
	 insert testAccount;

	 Test.StartTest(); 
	  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
	  LeadNewController  testAccPlan = new LeadNewController (sc);

	  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
	  pageRef.getParameters().put('RecordType', '22');
	  Test.setCurrentPage(pageRef);
	  testAccPlan.getRedir();
	 Test.StopTest();
 }

 
}

Let us know if this will help you