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
newbiewithapexnewbiewithapex 

How to write test class for the method that take user back?

I have method that used to go back by clicking cancel button. Can someone help me figure out test class for this method?
public pageReference back() {
    pageReference pf;
    if (ApexPages.currentPage().getParameters().get('Id') != null) {
        pf = new pageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    }
    return pf;
}

 
Raj VakatiRaj Vakati
Its simple .. you can call the back() method on the controller class instance 



   controller.back();
newbiewithapexnewbiewithapex
What you mean by controller class instance? 
Raj VakatiRaj Vakati
Give me a class and test class .. i will update the test class 
newbiewithapexnewbiewithapex
both classes are way too long to post here
Raj VakatiRaj Vakati
This is what you have to do 

YOURCLASSNAME  cont=new YOURCLASSNAME  (...) ;
cont.back();
newbiewithapexnewbiewithapex
Thank you it worked. It just didn't cover "pf = new pageReference('/' + ApexPages.currentPage().getParameters().get('Id'));" line
Raj VakatiRaj Vakati
Try this code
 
ApexPages.StandardController sc = new ApexPages.StandardController(pay);
     YOURCLASSNAME   cont = new YOURCLASSNAME  (sc);

     PageReference pageRef = Page.YOURPAGENAME;
     pageRef.getParameters().put('Id', YOURRECORDID );
cont.back();

 
newbiewithapexnewbiewithapex
Getting these errors: 
"Variable does not exist" for "ApexPages.StandardController sc = new ApexPages.StandardController(pay);" line
&
"Constructor not defined" for "YOURCLASSNAME   cont = new YOURCLASSNAME  (sc);" line

 
Raj VakatiRaj Vakati
Please pass the your controller object name to pay 

YOURCLASSNAME   in name of your class 
YOURRECORDID  is the record id of inserted record in test class