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.
1public class NewLeadController{2 private String firstName;3 private String lastName;4 private String company;5 private String email;67 public NewLeadController(){8}910 public String getFirstName(){11 return this.firstName;12}1314 public void setFirstName(String firstName){15 this.firstName = firstName;16}1718 public String getLastName(){19 return this.lastName;20}2122 public void setLastName(String lastName){23 this.lastName = lastName;24}2526 public String getCompany(){27 return this.company;28}2930 public void setCompany(String company){31 this.company = company;32}3334 public String getEmail(){35 return this.email;36}3738 public void setEmail(String email){39 this.email = email;40}4142 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}5455 if(p == null){56 p = Page.Success;57}5859 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@isTest2private class NewLeadTests{34 @isTest5 static void testAddNewLead(){6 PageReference pageRef = Page.NewLead;7 Test.setCurrentPage(pageRef);89 //Instantiate a new controller with no field values10 NewLeadController myController = new NewLeadController();11 String nextPage = myController.save().getUrl();1213 // Verify that the page fails to create the lead14 System.assertEquals('/apex/failure?error=noInsert', nextPage);1516 // Instantiate a new controller with valid field values17 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();2324 // Verify that the Success page displays25 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.
Next Steps
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.