Testing Your Controller Extentions


In a previous post I pointed it out how you can get good test coverage when deploying your VF pages. Since then many readers asked me about deploying VF pages with Controller Extensions.

In concept they are the same and the test is done with the same approach!

In this article I will show you how similar the Custom Controllers and Controller Extensions are. Very simple but maybe with a little twist!

Let's imagine that we have a Visualforce Page that extends the standard controller "Contact".

Here is how the page looks like:

<apex:page standardController="Contact" extensions="myControllerExtention">
<apex:form >
<apex:detail id="contactDetail" />
</apex:form>
</apex:page>



Very simple page, so when the page is viewed the URL will be like:
https://cs2.salesforce.com/apex/mypage?id=003200000005LaO

Where id parameter will be a contact's id and the user will see a page the same as Contact's detail page.

The controller looks like this:


public class myControllerExtention {

private Contact con;

public myControllerExtention(ApexPages.StandardController stdController)

{

this.con = (Contact)stdController.getRecord();

}

public string GetContactName()

{

return con.Name;

}

}


And now is the time we developed the Tester class.

The tester class should implement a method that creates an instance of the Contact controller and then pass that controller to the Constructor of our Extension class. That's it, the rest is the same as testing Custom controllers.


public class testMyPage {

static testMethod void myPage_Test()

{

//Test converage for the myPage visualforce page

PageReference pageRef = Page.MyPage;

Test.setCurrentPageReference(pageRef);

Account newAccount = new Account (name='XYZ Organization');

insert newAccount;

//create first contact

Contact myContact = new Contact (FirstName='Joe',

LastName='Schmoe',

AccountId=newAccount.id);

insert myContact;

ApexPages.StandardController sc = new ApexPages.standardController(myContact);

// create an instance of the controller

myControllerExtention myPageCon = new myControllerExtention(sc);

//try calling methods/properties of the controller in all possible scenarios

// to get the best coverage.

string ConName = myPageCon.GetContactName();

}

}






Happy coding!

7 comments:

  1. Thanks for your testing-tips.
    I tried this, but I reveice an error, when my controller is returning more than one value. I.e. in my getMyController class, I return a select with a lot off values which are used in a visual-force page.

    I tried to do it with the object itself, but the reult is a illegal assignment...

    ReplyDelete
  2. Perhaps Sam is more interested in showing off than actually helping out (sorry Sam)? Testing is by far the worst documented part of the Salesforce Apex documentation, this sample is great, a bit more info Sam if you will!?

    ReplyDelete
  3. Superb, been struggling with this for a while now, thankyou!

    ReplyDelete
  4. Awesome, i basically copied and pasted your test code and got the coverage for my controller.

    2011 and this post is still relevant. Thanks alot!! :)

    ReplyDelete
  5. This actually isn't quite right (although I don't know what the answer is).

    The test code should not do this step:

    ApexPages.standardController(myContact);

    because it's inserted the hand-crafted Contact object built in the testcode, but in reality the Contact object is created magically by the Salesforce framework and, crucially, may not have all the fields that the hand-crafted object does.

    The framework only pulls in the fields that have been referenced in the associated Visualforce page, not all fields in the object.

    ReplyDelete
  6. This was absolutely the article I was looking for. The examples I found on developer.force.com were way too in-depth and cluttered. You focused on just the Extension and your code comments were clear and plentiful. By following this article, I was able to go from 0% to 100% code coverage on my extension (with just a few tweaks) in a matter of minutes! Thank you so much!

    ReplyDelete
  7. Quite helpful thanks! I was trying to figure out how to test extensions that take a standardController object as an argument and this fit the bill perfectly.

    Appreciate you taking the time to write readable, simple examples.

    ReplyDelete