Testing PageReference in Controllers in Salesforce

Some developers have become frustrated when trying to write unit tests for their Controllers. To be fair, if you don’t know what base you assertions on, it can be hard to test the code. So here are a couple of quick tips for getting it done: Imaging you have a controller's pagereference method like the following:
public class TestingVFController_Controller{
public PageReference save(){
PageReference p=null;
if(this.qp==null || !'yy'.equals(this.qp)){
p=Page.pagenotfound;
p.getParameters().put('error','NoParam');
}
else{
p=Page.homepage;
}}


if qp parameter='yy' then this will redirect to /apex/homepage else /apex/pagenotfound.
for this test class would be

static testMethod void ControllerTest() {
// first test without parameter
PageReference p=Page.HomePage;
Test.setCurrentPage(p);
TestingVFController_Controller ctrl=new TestingVFController_Controller();
String nextPage=ctrl.save().getUrl();

System.assertEquals('/apex/pagenotfound?error=NoParam',nextPage);


//then check with parameter case

ApexPages.currentPage().getParameters().put('qp','yyyy');
ctrl=new TestingVFController_Controller();
nextPage=ctrl.save().getUrl();
System.assertEquals('/apex/homepage', nextPage);