0

I have a method like page reference method in my class. How to cover the pagereference method in test class??? all the other methods were covered except the pagereference method.

I tried like:

PageReference pageRef = Page.myclassname;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('timezonevar',var);

but of no use;

my methods is like:

public PageReference  mymethod(){

//here using some variables which are used in the standard controller constructor....

//all the logic goes here,

return null;
}
2
  • are you using standardController in page?
    – Ratan Paul
    Commented Jan 2, 2016 at 6:30
  • If your method always returns null then there is no need to return PageReference. You can make it a void-return method instead. Perhaps it would help more if you posted what the logic is. What is it that you want to test? The return value of the method or the logic it executes?
    – mkorman
    Commented Jan 3, 2016 at 18:10

1 Answer 1

3

If you are using custom controller

//Create test records 
PageReference pageRef = Page.myclassname;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('timezonevar',var);   
//init controller 
CustomCtrl objCtrl = new CustomCtrl();
//Call pageRef mymethod
PageReference   objPageRef =  objCtrl.mymethod();

//put system asserts

System.assertEquals (null,pageRef);

If you are using StandardController

//first create record

PageReference pageRef = Page.myclassname;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('timezonevar',var);        
// testRecord is your record
ApexPages.StandardController stc = new ApexPages.StandardController(testRecord);
//Call controller 
CustomCtrl objCtrl = new CustomCtrl(stc);
//Call pageRef mymethod
PageReference   objPageRef =  objCtrl.mymethod();
//put system asserts

System.assertEquals (null,pageRef);

You must log in to answer this question.

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