Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Visualforce Development Cookbook
  • Table Of Contents Toc
Visualforce Development Cookbook

Visualforce Development Cookbook - Second Edition

By : Keir Bowden
4.9 (8)
close
close
Visualforce Development Cookbook

Visualforce Development Cookbook

4.9 (8)
By: Keir Bowden

Overview of this book

Visualforce is a framework that allows developers to build sophisticated, custom user interfaces that can be hosted natively on the Force.com platform. The Visualforce framework includes a tag-based markup language, similar to HTML that is used to write the Visualforce pages and a set of controllers that are used to write business logic to the Visualforce pages. Visualforce Development Cookbook provides solutions to a variety of challenges faced by Salesforce developers and demonstrates how easy it is to build rich, interactive pages using Visualforce. Whether you are looking to make a minor addition to the standard page functionality or override it completely, this book will provide you with the help you require throughout. You will start by learning about the simple utilities and will build up to more advanced techniques for data visualization and to reuse functionality. You will learn how to perform various tasks such as creating multiple records from a single page, visualizing data as charts, using JavaScript to enhance client-side functionality, building a public website, and making data available to a mobile device. With an interesting chapter on tackling common issues faced while developing Visualforce pages, the book provides lots of practical examples to enhance and extend your Salesforce user interface.
Table of Contents (11 chapters)
close
close

Testing a controller extension

Controller extensions provide additional functionality for standard or custom controllers. The contract for a controller extension is that it provides a constructor that takes a single argument of the standard or custom controller that it is extending. Testing a controller extension introduces an additional requirement that an instance of the standard or custom controller, with appropriate internal state, is constructed before the controller extension.

In this recipe, we will create a controller extension to retrieve the contacts associated with an account managed by a standard controller and unit test the extension.

How to do it...

As the test class makes reference to the controller extension, this must be created first:

  1. Navigate to the Apex Classes setup page by clicking on Your Name|Setup|Develop|Apex Classes.
  2. Click on the New button.
  3. Paste the contents of the AccountContactsExt.cls Apex class from the code download into the Apex Class area.
  4. Click on the Save button.
  5. Create the unit test class by navigating to the Apex Classes setup page, clicking on Your Name|Setup|Develop|Apex Classes.
  6. Click on the New button.
  7. Paste the contents of the AccountContactsExtTest.cls Apex class from the code download into the Apex Class area.
  8. Click on the Save button.
  9. On the resulting page, click on the Run Tests button.

How it works...

The tests successfully execute, as shown in the following screenshot:

How it works...

Open the Developer Console by clicking on your name at the top right of the screen and selecting Developer Console from the resulting drop-down menu.

Select the Tests tab and you will see the test coverage information on the right-hand side, showing 100% code coverage for the AccountContactsExt class:

How it works...

The test class contains one unit test method. As unit tests do not have access to the organization data, the first task for the test is to set up the account and contact information:

Account acc=new Account(Name='Unit Test'); 
insert acc; 
     
List<Contact> contacts=new List<Contact>(); 
contacts.add(new Contact(FirstName='Unit',  
    LastName='Test', Email='[email protected]',  
    AccountId=acc.id)); 
contacts.add(new Contact(FirstName='Unit',  
    LastName='Test 2', Email='[email protected]', 
    AccountId=acc.id)); 
insert contacts; 

Next, the instance of the standard controller is instantiated:

ApexPages.StandardController std= 
  new ApexPages.StandardController(acc); 

Note that the StandardController requires the record that it is managing as a parameter to the constructor. As this is the record that will be made available to the controller extension, it must have the fields populated that the extension relies upon. In this case, the only field used by the extension is the ID of the account, and this is automatically populated when the account is inserted.

Note

In this recipe, the records to be tested are created in the test classes. In the real world, this is likely to lead to a lot of repetition and a maintenance overhead. In that case, a utility class to handle the setup of test data would be a more robust solution.

Finally, the controller extension is instantiated, taking the standard controller as a constructor parameter, and the test verifies that the extension has successfully retrieved the associated contacts:

AccountContactsExt controller=new AccountContactsExt(std); 
System.assertEquals(2, controller.contacts.size()); 

See also

  • The Testing a custom controller recipe in this chapter shows how to write unit tests for a custom controller that does not extend or rely upon another controller.
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Visualforce Development Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon