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 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 by 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:

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.

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='Unit.Test@Unit.Test', 
    AccountId=acc.id));
contacts.add(new Contact(FirstName='Unit', 
    LastName='Test 2', Email='Unit.Test2@Unit.Test',
    AccountId=acc.id));
insert contacts;

Next, the instance of the standard controller is instantiated.

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

Note that the standard controller requires the sObject 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.

Tip

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 set up 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.

Previous PageNext Chapter
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