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
Jan VandeveldeJan Vandevelde 

Need help with Testclass for custom controller which simply created a new lead from VF page on force.com sites?

Hi Salesforcedevs,

I need some help creating a testclass to test my custom controller. I never wriiten one for a controler before and wouldn't know how to "Push" the save button on the VF page to run the controller.

To explain:
VF page simply displays a apex:form with fields of the Lead object. No id's are passed through URL. Page is hosted on Force.com sites, just a blanc "create a lead form":
Page Tipformulier
 
<apex:page sidebar="false" showHeader="false" controller="Tipform" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">

<div id="wrapper">
    <div id="header">
        <img alt="Van Gansewinkel" class="auto-style1" src="{!$RESOURCE.HeaderTip800}" ></img>
    </div>
    
  <div class='scfForm'>
    <apex:form >
        <div id="tipgever">
            
            <div class="scfSectionContent">
                <div class="scfIntroBorder">
                In het onderstaande formulier kunt u de gegevens over uw tip invullen.<br />
                </div>
            <h2>Gegevens Tipgever</h2>
            <apex:outputLabel value="Voor- en achternaam *" />
            <apex:inputField value="{!lead.Naam_chauffeur__c}" required="true"/>
            <apex:outputLabel value="Regio *"/>
            <apex:inputField value="{!lead.Regio__c}" required="true"/>
            <apex:outputLabel value="E-mailadres"/>
            <apex:inputField value="{!lead.Privat_email__c}"/>
            <apex:outputLabel value="Vestiging Tipgever" />
            <apex:inputField value="{!lead.Planningslocatie_chauffeur__c}" />
            </div>
            
        </div>
        
        <div id="tip">
            
            <div class="scfSectionContent">
            <h2>Tipgegevens</h2>
            <apex:outputLabel value="Tip voor *"/>
            <apex:inputField value="{!lead.Lead_for__c}" required="true"/>
            <apex:outputLabel value="Type Tip *" />
            <apex:inputField value="{!lead.Tip_chauffeur__c}" required="true"/>
            <apex:outputLabel value="Iemand gesproken?"/>
            <apex:inputField value="{!lead.Contact_met_lead__c}"/>
            </div>
           
        </div>
        
        <div id="klant">
            
            <div class="scfSectionContent">
            <h2>Bedrijfsgegevens</h2>
            <apex:outputLabel value="Bedrijfsnaam *" />
            <apex:inputField value="{!lead.Company}" required="true"/>
            <apex:outputLabel value="Voornaam" />
            <apex:inputField value="{!lead.FirstName}" />
            <apex:outputLabel value="Achternaam *" />
            <apex:inputField value="{!lead.LastName}" />
            <apex:outputLabel value="E-mailadres" />
            <apex:inputField value="{!lead.Email}" />
            <apex:outputLabel value="Telefoon *" />
            <apex:inputField value="{!lead.Phone}" required="true"/>
            <apex:outputLabel value="Straat + huisnr *" />
            <apex:inputField value="{!lead.Street}" required="true"/>
            
            <apex:outputLabel value="Postcode" />
            <apex:inputField value="{!lead.PostalCode}"/>
           
            <apex:outputLabel value="Plaats *" />
            <apex:inputField value="{!lead.City}" onfocus="" required="true"/>
            <apex:outputLabel value="Land" />
            <apex:inputField value="{!lead.Country}"/>
            <apex:outputLabel value="Sector" />
            <apex:inputField value="{!lead.Industry}"/>
            </div>
           
        </div>
        
        <div id="bijkomend">
            
            <div class="scfSectionContent">
            <h2>Bijkomende Informatie</h2>
            <apex:outputLabel value="Omschrijving tip *"/>
            <apex:inputField value="{!lead.Omschrijving_tip__c}" required="true"/>
            <div class="scfSubmitButtonBorder">
            <apex:commandButton action="{!save}" value="Tip Indienen" />
            </div>
            </div>
            
        </div>    
        
    </apex:form>
</apex:page>

Custom Controller Tipform
public class Tipform {

// the Topform record we are adding values to
  public Lead lead {
    get {
      if (lead == null)
        lead = new Lead();
        lead.RecordTypeId ='012200000009dAI';
        lead.LeadSource = 'Tip form';
       
      return lead;
    }
    set;
  }

  public Tipform() {
    // blank constructor
  }

  // save button is clicked
  public PageReference save() {

    try {
      insert lead; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new tipform.'));
      return null;
    }

    // if successfully inserted new contact, then displays the thank you page.
    return Page.bedankt;
  }

}

So when someone comes on the URL he gets a blank form with leadfields he has to fill in and then hits Save. Lead is immediately created in Salesforce and the visitor is redirected to a thank you page.

I have really no clue how to write a testclass for this. Could someone please help me?

 
Best Answer chosen by Jan Vandevelde
Jan VandeveldeJan Vandevelde
It's OK, after some searching the web and finding a small exaple I figured it out and got 100% testcoverage ;-)

But if you want to give me feedback on it you're welcome to do so.
 
@isTest
private class TestTipformController {

    static testMethod void testingCreateNewLead() {
        Profile pf = [Select Id from Profile where Name = 'System Administrator'];
        
 
        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
 
        system.runAs(u){
        	Test.startTest();
        	PageReference pr = Page.tipformulier;
        	Test.setCurrentPageReference(pr);
        	Tipform cc = new Tipform();
        	
        	//Fill in leadfields
        	cc.lead.Naam_chauffeur__c = 'TestController Tipform Jan';
        	cc.lead.Regio__c = 'West';
        	cc.lead.LastName = 'TestController Tipform Jan';
        	cc.lead.Lead_for__c = 'Prospect';
        	cc.lead.Tip_chauffeur__c = 'Nieuwe activiteit';
        	cc.lead.Company = 'TestController Tipform Jan';
        	cc.lead.Phone = '+32472584767';
        	cc.lead.Street = 'Ninoofsesteenweg 42';
        	cc.lead.City = 'Denderhoutem';
        	cc.lead.Omschrijving_tip__c = 'Testomschrijving tipformulier';
        	cc.save();
        	       	
        	Test.stopTest();
        	
        	//get the newly created lead
        	List<Lead> l = [Select Id, LastName from Lead where LastName = 'TestController Tipform Jan'];
        	System.debug(l);
        	System.assertEquals(1, l.size());
        	
        	
        }
    }
    
    static testMethod void testingFailedNewLead() {
        Profile pf2 = [Select Id from Profile where Name = 'System Administrator'];
        
 
        User u2 = new User();
        u2.FirstName = 'Test2';
        u2.LastName = 'User2';
        u2.Email = 'testuser2@test123456789.com';
        u2.CompanyName = 'test.com';
        u2.Title = 'Test User';
        u2.Username = 'testuser2@test123456789.com';
        u2.Alias = 'testuser';
        u2.CommunityNickname = 'Test User';
        u2.TimeZoneSidKey = 'America/Mexico_City';
        u2.LocaleSidKey = 'en_US';
        u2.EmailEncodingKey = 'ISO-8859-1';
        u2.ProfileId = pf2.Id;
        u2.LanguageLocaleKey = 'en_US';
        insert u2;
 
        system.runAs(u2){
        	Test.startTest();
        	PageReference pr2 = Page.tipformulier;
        	Test.setCurrentPageReference(pr2);
        	Tipform cc2 = new Tipform();
        	
        	//Fill in leadfields
        	cc2.lead.Naam_chauffeur__c = 'TestController2 Tipform Jan';
        	cc2.lead.Regio__c = 'West';
        	cc2.lead.Lead_for__c = 'Prospect';
        	cc2.lead.Tip_chauffeur__c = 'Nieuwe activiteit';
        	cc2.lead.Company = 'TestController2 Tipform Jan';
        	cc2.lead.Phone = '+32472584767';
        	cc2.lead.Street = 'Ninoofsesteenweg 42';
        	cc2.lead.City = 'Denderhoutem';
        	cc2.lead.Omschrijving_tip__c = 'Testomschrijving tipformulier';
        	cc2.save();
        	       	
        	Test.stopTest();
        	
        	//get the newly created lead
        	List<Lead> l2 = [Select Id, LastName from Lead where LastName = 'TestController2 Tipform Jan'];
        	System.debug(l2);
        	System.assertEquals(0, l2.size());
        	
        	
        }
    }
}