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.
As the controller is reacting to parameters on the URL, the page reference must be set up and populated with the name
parameter.
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.
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.
The test then sets the search term, executes the search method, and confirms the results.