Test a controller 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:
suppose you have a controller like this


public with sharing class TestingVFController_Controller {

private string qp;

public TestingVFController_Controller(){
this.qp=ApexPages.currentPage().getParameters().get('qp');
}

public string FirstName {
get;set;}

public string LastName {
get;set;
}
public string email {
get;set;
}
public string Company {
get;set;
}

public PageReference save(){
PageReference p=null;
if(this.qp==null || !'yyyy'.equals(this.qp)){
p=Page.filenotfound;
p.getParameters().put('error','NoParam');

}
else{
try{
Lead l = new Lead(Firstname=this.firstName,LastName=this.LastName,company=this.Company, email=this.email);
insert l;
}
catch(Exception e)
{
p=Page.filenotfound;
p.getParameters().put('error','insertFailure');
}

}
if(p==null)
p=Page.homepage;

p.setRedirect(true);
return p;
}
}

No comments:

Post a Comment