You need to sign in to do that
Don't have an account?
Need Help Creating Test Method for Web2Lead Extension
I am newbie and found out when I tried to deploy to production, I need to create a test method for the following code. This code came from a cookbook and it works fabulous in staging, but I can't deploy to production because it returns 0% test coverage. Any help would be appreciated!
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;
p.setRedirect(true);
return p;
}
}
Here is my Visualforce page:
<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="true">
<apex:composition template="{!$Site.Template}">
<apex:define name="body">
<apex:form >
<apex:messages id="error" styleClass="errorMsg" layout="table" style="margin-top:1em;"/>
<apex:pageBlock title="" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveLead}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Contact us" collapsible="false" columns="1">
<apex:inputField value="{!Lead.FirstName}" required="true"/>
<apex:inputField value="{!Lead.LastName}"/>
<apex:inputField value="{!Lead.Title}" required="true"/>
<apex:inputField value="{!Lead.Company}" />
<apex:inputField value="{!Lead.Phone}" required="true"/>
<apex:inputField value="{!Lead.Email}" required="true"/>
<apex:inputField value="{!Lead.City}" required="true"/>
<apex:inputField value="{!Lead.State}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:define>
</apex:composition>
</apex:page>
Hi,
You need to simulate what the user would do in code:
- populate a lead
- instantiate a controller extension (a bit like putting the lead data in the form)
- call the saveLead method
You should test good and bad scenarios.
Notice the second method is trying to save a lead without lastname.
If you have other validations, your test results may vary, make sure that you add all the necessary fields in the first method.
@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);
}
}
All Answers
Hi,
You need to simulate what the user would do in code:
- populate a lead
- instantiate a controller extension (a bit like putting the lead data in the form)
- call the saveLead method
You should test good and bad scenarios.
Notice the second method is trying to save a lead without lastname.
If you have other validations, your test results may vary, make sure that you add all the necessary fields in the first method.
@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);
}
}
This worked perfect! 100% test coverage. Thank you so much!