Reader small image

You're reading from  Visualforce Development Cookbook

Product typeBook
Published inSep 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781782170808
Edition1st Edition
Languages
Right arrow
Author (1)
Keir Bowden
Keir Bowden
author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden

Right arrow

Testing a custom controller


Writing unit tests for Visualforce page controllers is often a source of confusion for developers new to the technology. A common mistake is to assume that the page must somehow be rendered and interacted with in the test context, whereas, in reality the page is very much a side issue. Instead, tests must instantiate the controller and set its internal state as though the user interaction had already taken place, and then execute one or more controller methods and confirm that the state has changed as expected.

In this recipe we will unit test SearchFromURLController from the Reacting to URL parameters recipe.

Getting ready

This recipe requires that you have already completed the Reacting to URL parameters recipe, as it relies on SearchFromURLController being present in your Salesforce instance.

How to do it…

  1. Create the unit test class by navigating to the Apex Classes setup page and by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the SearchFromURLControllerTest.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. On the resulting page, click on the Run Tests button.

How it works…

The tests successfully execute as shown in the following screenshot:

Navigating back to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes shows that the tests have achieved 100 percent coverage of the controller.

Tip

Percentage coverage is important as at least 75 percent coverage across all code must be achieved before classes may be deployed to a production organization.

The test class contains two unit test methods. The first method tests that the search is correctly executed when the search term is passed on the page URL. As unit tests do not have access to organization data, the first task for the test is to set up three test accounts.

List<Account> accs=new List<Account>();
accs.add(new Account(Name='Unit Test'));
accs.add(new Account(Name='Unit Test 2'));
accs.add(new Account(Name='The Test Account'));
insert accs;

As the controller is reacting to parameters on the URL, the page reference must be set up and populated with the name parameter.

PageReference pr=Page.SearchFromURL;
pr.getParameters().put('name', 'Unit');
Test.setCurrentPage(pr);

Finally, the controller is instantiated, which causes the action method that executes the search to be invoked from the constructor. The test method then confirms that the search was executed and the actual number of matches equals the expected number.

SearchFromURLController controller=new 
         SearchFromURLController();
System.assertEquals(true, controller.searched);
System.assertEquals(2, controller.accounts.size());

The second unit test method tests that the search is correctly executed when the user enters a search term. In this case, there is no interaction with the information on the page URL, so the test simply instantiates the controller and confirms that no search has been executed by the constructor.

SearchFromURLController controller=new SearchFromURLController();
System.assertEquals(false, controller.searched);

The test then sets the search term, executes the search method, and confirms the results.

controller.name='Unit';
System.assertEquals(null, controller.executeSearch());
System.assertEquals(2, controller.accounts.size());

See also

  • The Testing a controller extension recipe in this chapter shows how to write unit tests for a controller that extends a standard or custom controller.

Previous PageNext Page
You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013Publisher: PacktISBN-13: 9781782170808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden