function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
XVLXVL 

Can someone help me write a test class for my visualforce extension?

Here is the extenstion for my lead controller?

public class myWeb2LeadExtension {



    private final Lead weblead;



    public myWeb2LeadExtension(ApexPages.StandardController stdController) {

       weblead = (Lead)stdController.getRecord();

    }

    

     public PageReference saveLead() {

       try {
       weblead.whitepaperID__c = Apexpages.currentPage().getParameters().get('cid');

       insert(weblead);

       }

       catch(System.DMLException e) {

           ApexPages.addMessages(e);

           return null;

       }

     PageReference p = Page.ThankYouPageWhitePaper;
     p.getParameters().put('cid', Apexpages.currentPage().getParameters().get('cid'));
 

       p.setRedirect(true);

       return p;
      
    }
   
}
XVLXVL
Here is my form. I am using a visualforce form instead of web2lead.



apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="false">
  <apex:define name="body">
    <apex:pageBlock title="" mode="edit">

      <script type="text/javascript">
      window.onload=function() {
          var queryDict = {}
          var cid = '';
          location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})
          if(typeof queryDict['cid'] != 'undefined') {
            cid = queryDict['cid'];
          }
          var pageTitle = '';
          var pageDesc = '';
          var pageBody = '';
          switch(cid) {
case 'AnalyticInnovations':
              pageTitle = 'title';
              pageDesc = '';
              pageBody = 'body'
break;

default:
              cid = ''; // Protect against injection
              pageBody = '<p style="color:red; font-weight:bold;">Missing or incorrect whitepaper.</p>';
              document.getElementById('contact-form').style.display = 'none';
              break;
          }

         
          document.getElementById('pageTitle').innerHTML = pageTitle;
          document.getElementById('pageDesc').innerHTML = pageDesc;
          document.getElementById('pageBody').innerHTML = pageBody;
          if(cid != '') {
            document.getElementsByTagName('form')[0].action += '?cid=' + cid;
          }
      }
      </script>



      <div style="width:1024px;">
        <div style="float:left; width:650px;">
          <div id="pageTitle" style="font-weight:bold; font-size:24px;"></div>
          <div id="pageDesc" style="font-weight:bold; font-size:18px"></div>
          <div id="pageBody"></div>
        </div>
        <div style="float:right; width:325px; padding-left:45px;">
          <div id="contact-form">
            <apex:form >
              <apex:messages id="error" styleClass="errorMsg" layout="table" style="margin-top:1em; color:red;"/>
              <apex:pageBlockSection title="Contact Us" collapsible="false" columns="1">
                 
                  <apex:inputField value="{!Lead.Title}"/>
                  <apex:inputField value="{!Lead.FirstName}"/>
                  <apex:inputField value="{!Lead.LastName}"/>
                  <apex:inputField value="{!Lead.Email}"/>
                  <apex:inputField value="{!Lead.Phone}"/>
                  <apex:inputField value="{!Lead.Company}"/>
                  <apex:inputField value="{!Lead.Street}"/>
                  <apex:inputField value="{!Lead.City}"/>
                  <apex:inputField value="{!Lead.State}"/>
                  <apex:inputField value="{!Lead.PostalCode}"/>
                  <apex:inputField value="{!Lead.CountryDropDown__c}"/>
                  <apex:commandButton value="Submit" action="{!saveLead}"/>
              </apex:pageBlockSection>
            </apex:form>
          </div>
        </div>
      </div>

    </apex:pageBlock>
  </apex:define>
</apex:page>


I got started on a test class, but can only get  46 percent coverage.....this is my test class.


@isTest private class testLeadClass{

//positive test
static void saveLeadTestOK(){
        
         // populate a lead
         Lead l = new Lead(company='testcompany',lastname='testln',whitepaperID__c='TamingData');
         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.ThankYouPageWhitePaper.getUrl(),p.getUrl());
        
        
     }
    
//negative test
static 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);
        
     }
}