Find your content:

Search form

You are here

How should I build test methods for Visualforce Controller Extensions

 
Share

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

    }
}

Attribution to: Born2BeMild

Possible Suggestion/Solution #1

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.


Attribution to: Tom Gersic

Possible Suggestion/Solution #2

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.


Attribution to: metadaddy

Possible Suggestion/Solution #3

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. 

}

Attribution to: jordan.baucke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/104

My Block Status

My Block Content