To ensure error-free code, create and execute Apex unit tests for every custom controller and controller extension that you write. Unit tests are class methods that verify whether a particular piece of code works properly. Unit test methods take no arguments, commit no data to the database, and are flagged with the @isTest annotation in the method definition.
Before You Begin
The example NewLead page creates a lead from the form data that the user provides. If the user completes all the fields with valid values, the new lead is created, and the user is directed to the Success page. Otherwise, a new lead isn’t created, and the user is directed to the Failure page.
-
Create a custom controller named
NewLeadController.public class NewLeadController { private String firstName; private String lastName; private String company; private String email; public NewLeadController() { } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCompany() { return this.company; } public void setCompany(String company) { this.company = company; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public PageReference save() { PageReference p = null; try { Lead newlead = new Lead(LastName=this.lastName, FirstName=this.firstName, Company=this.company, Email=this.email); insert newlead; } catch (Exception e) { p = Page.Failure; p.getParameters().put('error', 'noInsert'); } if (p == null) { p = Page.Success; } p.setRedirect(true); return p; } } -
Create a page named
NewLead.<apex:page controller="NewLeadController" tabstyle="lead"> <apex:pageBlock> <apex:form> <h1>Test page for adding leads</h1> <p>This is a test page for adding leads.</p> <p>First name: <apex:inputText value="{!FirstName}"></apex:inputText></p> <p>Last name: <apex:inputText value="{!LastName}"></apex:inputText></p> <p>Company: <apex:inputText value="{!Company}"></apex:inputText></p> <p>Email address: <apex:inputText value="{!Email}"></apex:inputText></p> <apex:commandButton action="{!save}" value="Save New Lead"/> </apex:form> </apex:pageBlock> </apex:page> -
Create a page named
Success.<apex:page> Success! </apex:page> -
Create a page named
Failure.<apex:page> A failure has occurred. </apex:page> -
Create a test class named
NewLeadTeststhat contains the test methodtestAddNewLead. The test checks both failure and success conditions. To set the currentPageReferencefor the controller, use the methodTest.setCurrentPage(PageReference page).@isTest private class NewLeadTests { @isTest static void testAddNewLead() { PageReference pageRef = Page.NewLead; Test.setCurrentPage(pageRef); //Instantiate a new controller with no field values NewLeadController myController = new NewLeadController(); String nextPage = myController.save().getUrl(); // Verify that the page fails to create the lead System.assertEquals('/apex/failure?error=noInsert', nextPage); // Instantiate a new controller with valid field values myController = new NewLeadController(); myController.setLastName('lastname'); myController.setFirstName('firstname'); myController.setCompany('Ursa Major'); myController.setEmail('firstlast@ursamajor.com'); nextPage = myController.save().getUrl(); // Verify that the Success page displays System.assertEquals('/apex/success', nextPage); Lead[] leads = [select id, email from lead where Company = 'Ursa Major']; System.assertEquals('firstlast@ursamajor.com', leads[0].email); } } -
Run the Apex test method in your chosen developer environment.
You can run Apex test methods in the Developer Console, in Setup, in the Salesforce extensions for Visual Studio Code, or using the API. See Run Unit Test Methods in the Apex Developer Guide.
Next Steps
tip: When testing, if the console shows the message Method does not exist or incorrect signature: Test.setCurrentPage(System.PageReference), check whether you created a class called Test. If so, rename the class.
See Also