You need to sign in to do that
Don't have an account?

test class coverage
Hi All,
My class is not getting covered in below red lines.Any help in fixing in test class
public class Web2LeadExtension {
public Lead wlead{
get {
if (wlead== null)
wlead= new Lead ();
return wlead;
}
set;
}
public Web2LeadExtension(ApexPages.StandardController
stdController) {
wlead = (Lead)stdController.getRecord();
}
public PageReference saveLead() {
try {
insert(wlead);
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
PageReference p = Page.mypage;
return p;
}
}
@isTest private class testLeadClass{
static testmethod void saveLeadTest(){
Lead l = new Lead(company='test company',lastname='xyz');
test.starttest();
Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));
PageReference p = ext.saveLead();
test.stoptest();
system.assertEquals(Page.mypage.getUrl(),p.getUrl());
}
}
Thanks
My bad, needs to be at least:
Although as you have it modified should be fine. I think the use case you've got in your extension is a bit broad, checking for wlead shouldn't be necessary as you can't get a null from the StandardController's getRecord. It's either the record defined by the Id or a new Lead() by nature.
Also, DMLException may be to specific in the catch. Try the extension like this:
See if that helps.
All Answers
Try another test method where:
Lacks the incoming sObject, so:
This will both hit the instance where webLead should start as null and throw a DMLException (validation rules) ... I think.
Thanks joshbirk for you reply. I am getting a save error Constructor not defined
@isTest private class testLeadClass{
static testmethod void saveLeadTest(){
Lead l = new Lead(company='test company',lastname='xyz');
test.starttest();
Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));
PageReference p = ext.saveLead();
test.stoptest();
system.assertEquals(Page.mypage.getUrl(),p.getUrl());
}
static testmethod void saveLeadTest2(){
// Lead l = new Lead(company='testcompany');
test.starttest();
Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController());
PageReference p = ext.saveLead();
test.stoptest();
system.assertEquals(null,p);
}
}
static testmethod void saveLeadTest2(){
Lead l = new Lead(company='testcompany');
test.starttest();
Web2LeadExtension ext = new Web2LeadExtension(new ApexPages.StandardController(l));
PageReference p = ext.saveLead();
test.stoptest();
system.assertEquals(null,p);
}
I am getting test class failure when i changed to the above code. "Required field missing Last Name"
My bad, needs to be at least:
Although as you have it modified should be fine. I think the use case you've got in your extension is a bit broad, checking for wlead shouldn't be necessary as you can't get a null from the StandardController's getRecord. It's either the record defined by the Id or a new Lead() by nature.
Also, DMLException may be to specific in the catch. Try the extension like this:
See if that helps.
Thanks joshbirk.