+ Start a Discussion
Puja ChoudharyPuja Choudhary 

test class for redirecting to another page with parameters

I want to write test class for my ctr_homepage class.
public with sharing class ctr_homepage {
     // Varaible Declaration
    Public String tablen{get;set;}
    Public String tout { get; set;}
    
     /* Constructor Starts here */ 
    Public ctr_homepage(){
        tout = System.currentPageReference().getParameters().get('tout');
        tablen = ApexPages.currentPage().getParameters().get('tablen');
    }
      /* Constructor ends here */ 
 
    // To redirect to english Language
    public PageReference english() {
        PageReference engpg = new PageReference('/apex/vfp_validation?lang=en&tout='+tout+'&tablen='+tablen);
        engpg.setRedirect(TRUE);
        return engpg ;
    }

   // To redirect to hindi Language
    public PageReference hindi() {
        PageReference hinpg = new PageReference('/apex/vfp_validation?lang=hi');
        hinpg.setRedirect(TRUE);
        return hinpg ;
    }
    
    // To redirect to bengali Language
    public PageReference bengali() {
       PageReference benpg = new PageReference('/apex/vfp_validation?lang=bn');
        benpg.setRedirect(TRUE);
        return benpg ;
    }

}

I am not able to understand what i should write for the PageReference.
Best Answer chosen by Puja Choudhary
Karan Shekhar KaulKaran Shekhar Kaul
Just call the pageference methods and use assertequal to check if it is redirected to correct URl.
ctr_homepage  instance = new ctr_homepage ();
PageReference benpg =  instance.bengali();
system.assertEqual(benpg.getUrl(),'/apex/vfp_validation?lang=bn');

Alternatively, you can use other page reference methods.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helpd resolve your issue.
 

All Answers

Karan Shekhar KaulKaran Shekhar Kaul
Just call the pageference methods and use assertequal to check if it is redirected to correct URl.
ctr_homepage  instance = new ctr_homepage ();
PageReference benpg =  instance.bengali();
system.assertEqual(benpg.getUrl(),'/apex/vfp_validation?lang=bn');

Alternatively, you can use other page reference methods.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helpd resolve your issue.
 
This was selected as the best answer
Puja ChoudharyPuja Choudhary
thank you @Karan Shekhar Kaul for the help :D