Newer Version Available
Test a Custom Controller
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.
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.
1public class NewLeadController { 2 private String firstName; 3 private String lastName; 4 private String company; 5 private String email; 6 7 public NewLeadController() { 8 } 9 10 public String getFirstName() { 11 return this.firstName; 12 } 13 14 public void setFirstName(String firstName) { 15 this.firstName = firstName; 16 } 17 18 public String getLastName() { 19 return this.lastName; 20 } 21 22 public void setLastName(String lastName) { 23 this.lastName = lastName; 24 } 25 26 public String getCompany() { 27 return this.company; 28 } 29 30 public void setCompany(String company) { 31 this.company = company; 32 } 33 34 public String getEmail() { 35 return this.email; 36 } 37 38 public void setEmail(String email) { 39 this.email = email; 40 } 41 42 public PageReference save() { 43 PageReference p = null; 44 try { 45 Lead newlead = new Lead(LastName=this.lastName, 46 FirstName=this.firstName, 47 Company=this.company, 48 Email=this.email); 49 insert newlead; 50 } catch (Exception e) { 51 p = Page.Failure; 52 p.getParameters().put('error', 'noInsert'); 53 } 54 55 if (p == null) { 56 p = Page.Success; 57 } 58 59 p.setRedirect(true); 60 return p; 61 } 62} -
Create a page named NewLead.
1<apex:page controller="NewLeadController" tabstyle="lead"> 2 <apex:pageBlock> 3 <apex:form> 4 <h1>Test page for adding leads</h1> 5 <p>This is a test page for adding leads.</p> 6 <p>First name: <apex:inputText value="{!FirstName}"></apex:inputText></p> 7 <p>Last name: <apex:inputText value="{!LastName}"></apex:inputText></p> 8 <p>Company: <apex:inputText value="{!Company}"></apex:inputText></p> 9 <p>Email address: <apex:inputText value="{!Email}"></apex:inputText></p> 10 <apex:commandButton action="{!save}" value="Save New Lead"/> 11 </apex:form> 12 </apex:pageBlock> 13</apex:page> -
Create a page named Success.
1<apex:page> 2 Success! 3</apex:page> -
Create a page named Failure.
1<apex:page> 2 A failure has occurred. 3</apex:page> -
Create a test class named NewLeadTests that contains the test method
testAddNewLead. The
test checks both failure and success conditions. To set the
current PageReference
for the controller, use the method Test.setCurrentPage(PageReference
page).
1@isTest 2private class NewLeadTests { 3 4 @isTest 5 static void testAddNewLead() { 6 PageReference pageRef = Page.NewLead; 7 Test.setCurrentPage(pageRef); 8 9 //Instantiate a new controller with no field values 10 NewLeadController myController = new NewLeadController(); 11 String nextPage = myController.save().getUrl(); 12 13 // Verify that the page fails to create the lead 14 System.assertEquals('/apex/failure?error=noInsert', nextPage); 15 16 // Instantiate a new controller with valid field values 17 myController = new NewLeadController(); 18 myController.setLastName('lastname'); 19 myController.setFirstName('firstname'); 20 myController.setCompany('Ursa Major'); 21 myController.setEmail('firstlast@ursamajor.com'); 22 nextPage = myController.save().getUrl(); 23 24 // Verify that the Success page displays 25 System.assertEquals('/apex/success', nextPage); 26 Lead[] leads = [select id, email from lead where Company = 'Ursa Major']; 27 System.assertEquals('firstlast@ursamajor.com', leads[0].email); 28 } 29} -
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.