Testing Visualforce Controller Extension

Controller Extension
A controller extension is an Apex class that extends the functionality of a standard or custom controller. The class constructor takes a single argument of type ApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller you want to extend.
The extension is associated with the page using the extensions attribute of <apex:page> the component
For example: <apex:page standardController=”Account” extensions=”AccountControllerExtension”>


Testing the Controller Extension
Controller extensions like all Apex codes should be covered by unit tests. Unit tests are class methods that verify whether a particular piece of code is working correctly as expected. Apex unit test methods take no arguments, commit no data to the database, and are decorated with the @isTest annotation in the method definition.
Consider the controller extension below:

public class AccountControllerExtension {
    Account acct;
    //the controller extension constructor
    public AccountControllerExtension(ApexPages.StandardController controller) {
             this.acct = (Account) controller.getRecord();
    }
       public List<Opportunity> getChildOpps() {
        return [Select Name, Amount, CloseDate, StageName
        From Opportunity
        Where AccountId =:acct.Id
        and (IsWon = true or IsClosed = false)];
     }
}

The markup named AccountNew below uses the above controller:

<apex:page standardController="Account"
extensions="AccountControllerExtension">
 <apex:form >
    <apex:pageBlock >
      <apex:pageblockButtons >
        <apex:commandButton action="{!save}" value="save" />
        </apex:pageblockButtons>
        <apex:inputField value="{!Account.Rating}" />
        <apex:pageblocktable value="{!childOpps}" var="op">
            <apex:column value="{!op.Name}"/>
            <apex:column value="{!op.Amount}"/>
            <apex:column value="{!op.StageName}"/>
            <apex:column value="{!op.CloseDate}"/>
        </apex:pageblocktable>    
      </apex:pageBlock>
</apex:form>

</apex:page>

Comment: The Visualforce angle bracket in the above code changed to &lt; because the Custom Html block is not working. I’m working to resolve it

The Apex Test class below test the controller

@isTest
public class AccountControllerExtensionTest {

    @isTest static void testAccountOpportunity(){    
       Account ac = new Account(Name = 'Test Account');   
       insert ac;       
      //AccountNew is the Visualforce page created
       PageReference testPage = Page.AccountNew; 
       Test.setCurrentPage(testPage);
       testPage.getParameters().put('Id', String.valueOf(ac.Id));
       
       ApexPages.StandardController sc = new  ApexPages.StandardController(ac);     
       AccountControllerExtension ext = new AccountControllerExtension(sc);         
        
       System.debug(ext.getChildOpps()); 
       List<Account> al = [Select Name from Account where Name LIKE '%Test%' LIMIT 1];
       System.assertEquals(1, al.size());
   } 
  }  

Running the test gave 100% test coverage. Click the Test tab to check the status of the TestRun. Expand it to see the test class and the method result like the image below:

Test run status

Resources
Apex Developer Guide
Visualforce Developer Guide

This Post Has One Comment

Leave a Reply