28

I would like to know the best way to write test methods for controller extension classes that are used by Visualforce.

What is the minimum set code that I need to cover the basic operation of the controller without taking into account any custom functionality that I might add.

Here is my example class with the test method ready to be completed.

public with sharing class myExtension{

    private final Account acct;

    public myExtension(ApexPages.StandardController stdController){
        this.acct = (Account)stdController.getRecord();
    }

    public static testMethod void testmyExtension(){

    }
}

3 Answers 3

42

The following code will test that you are correctly saving the account record in the constructor (which is all your extension does so far):

public static testMethod void testmyExtension() {
    Account a = new Account(name='Tester');
    insert a;
    ApexPages.StandardController sc = new ApexPages.standardController(a);
    myExtension e = new myExtension(sc);
    System.assertEquals(e.acct, a);
}

As you add more functionality to your extension, take a look at Testing Custom Controllers and Controller Extensions.

2
  • 1
    haha basically the same thing at the same time...this town ain't big enough for the both of us. Aug 1, 2012 at 22:56
  • 1
    In addition to MetaDaddy's post, it's good practice to test all your methods in separate test methods. That way you can test all the conditions (null, empty strings, all logic branches, etc) for each method. So if you have four action methods, you should have at least 5 test methods (1 constructor + 4 methods). GL testing. Aug 20, 2012 at 16:44
8

So to test the basic invocation of your standard controller start by creating a record of the type you want for the standard controller, than create a standard controller type for that object:

@isTest
public static void testStandardControll(){
    // create an account record (not sure if these are all required fields)
    Account account = new Account(Name='Account Name'); 

    // create a new Account standard controller by passing it the account record
    ApexPages.StandardController controller = new ApexPages.StandardController(account);

    // now pass it to the extension
    myExtension stdController = new myExtension(controller);

    system.assert(stdController != null); // controller has successfully been created

    // .. now test your extension methods, checking validity of results and code coverage. 

}
1
  • 3
    You can do better than System.assert(stdController != null), Jordan - see my answer :-)
    – metadaddy
    Aug 1, 2012 at 22:57
2

Obviously you need to cover at least 75% of code at a minimum. Setting the bar higher than that is a good practice. In addition, you should write tests that run code with various different Profiles to make sure it works properly as users other than system admins.

Beyond that, for the most part I don't see a lot of value in writing tests that test base Salesforce functionality--for instance, there's not much value in testing that a child record in a Master-Detail relationship always has a parent. Or that an update operation does, in fact, actually update a record. I see more value focusing on writing tests that test the custom functionality that you have created with your code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .