You need to sign in to do that
Don't have an account?
ktshannon
Problem with testClass for Sites To Lead Form Cookbook code.
Hey all,
I am having issues creating a testClass for a sample code from the Cookbook.
http://developer.force.com/cookbook/recipe/creating-a-web-to-lead-form-for-your-force-com-site
The controller is very simple and can be seen here:
public class myWeb2LeadExtension {
private final Lead weblead;
public myWeb2LeadExtension(ApexPages.StandardController
stdController) {
weblead = (Lead)stdController.getRecord();
}
public PageReference saveLead() {
try {
insert(weblead);
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
PageReference p = Page.thankyou_getinvolved;
p.setRedirect(true);
return p;
}
}
I have written the testClass to insert all required fields for the Lead, but it is still not validating the class. What am I missing?
Thank you for your help!
Can you post your testClass / unit test?
@isTest private class testLeadClass{
//positive test
static testmethod void saveLeadTestOK(){
// populate a lead
Lead l = new Lead(company='testcompany',lastname='testln');
test.starttest();
// instantiate a controller extension
myWeb2LeadExtension ext = new myWeb2LeadExtension(new ApexPages.StandardController(l));
// call the saveLead method
PageReference p = ext.saveLead();
test.stoptest();
// make sure that everything worked as expected and the user is redirected to the Thank You page
system.assertEquals(Page.ThankYou.getUrl(),p.getUrl());
}
//negative test
static testmethod void saveLeadTestKO(){
Lead l = new Lead(company='testcompany');
test.starttest();
myWeb2LeadExtension ext = new myWeb2LeadExtension(new ApexPages.StandardController(l));
PageReference p = ext.saveLead();
test.stoptest();
system.assertEquals(null,p);
}
}
Any change in behavior if the assertEquals occurs before stopTest?
No Change in behavior
When you say it isn't validating the class, do you mean there's specifc code coverage not getting hit? Or do the assertEquals fail?
Hi joshbirk,
my test class is for ktshannon's class.Sorry for whole confusion.
Thanks
Ah ha! That makes more sense...
Hi joshbirk,
I am having coverage issue.Can you look into my post.
http://boards.developerforce.com/t5/Apex-Code-Development/test-class-coverage/td-p/338073
Thanks