How should I build test methods for Visualforce Controller Extensions

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(){



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.

Source : Link , Question Author : Born2BeMild , Answer Author : metadaddy

Leave a Comment