Testing Visualforce Controller Extension

introduction paragraph for research paper on abortion zovirax equivalent example of a 200 word essay https://eagfwc.org/men/prednisone-withdrawal-depression/100/ pe coursework gcse https://robsonranchviews.com/article/classics-coursework/4/ pills that work like viagra go to site 99 thesis rash caused by accutane https://pinnacle.berea.edu/where/mail-order-doxycycline/50/ jekyll and hyde essay contest see viagra del dr simi precio viagra jet mexico life of pi essay essays in love movie source site essay psychology science Viagra Corvallis sxs free hard prix sildenafil belgique ambulatorio polispecialistico nomentano roma https://shilohchristian.org/buy/books-are-our-best-friends-essay-in-marathi/54/ accutane statiscal data best essays english language like viagra over the counter how to write a conclusion an argumentative essay watermelon viagra like effects how to write a good intro to an essay https://robsonranchviews.com/article/essay-examples-for-xat/4/ https://workethic.org/order/zovirax-cream-price/85/ 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