Apex Test Class for Visual Force (VF) Pages Controller Extension : jayakrishnasfdc
by: jayakrishnasfdc
blow post content copied from Jayakrishna Ganjikunta
click here to view original post

Controller extensions and custom controllers, like all Apex scripts, should be covered by unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, and are flagged with the testMethod keyword in the method definition.
When writing unit tests for controller extension and custom controller classes, you can set query parameters that can then be used in the tests.
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 < 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()); } }
Another Example:
Reference from http://amitsalesforce.blogspot.com/
Test Class for Standard Controller
@isTest public class ExtensionTestClass { static testMethod void testMethod1() { Account testAccount = new Account(); testAccount.Name='Test Account record' ; insert testAccount; Test.StartTest(); ApexPages.StandardController sc = new ApexPages.StandardController(testAccount); myControllerExtension testAccPlan = new myControllerExtension(sc); PageReference pageRef = Page.AccountPlan; // Add your VF page Name here pageRef.getParameters().put('id', String.valueOf(testAccount.Id)); Test.setCurrentPage(pageRef); //testAccPlan.save(); call all your function here Test.StopTest(); } }
Test Class for Controller class
@isTest public class ControllerTestClass { static testMethod void testMethod1() { Account testAccount = new Account(); testAccount.Name='Test Account record' ; insert testAccount; Test.StartTest(); PageReference pageRef = Page.AccountPlan; // Add your VF page Name here pageRef.getParameters().put('id', String.valueOf(testAccount.Id)); Test.setCurrentPage(pageRef); myController testAccPlan = new myController(); //testAccPlan.save(); call all your function here Test.StopTest(); } }
Test Class for StandardSetController
@isTest public class TestStandardSetController { static testMethod void testMethod1() { List <Account> lstAccount = new List<Account>(); Account testAccount = new Account(); testAccount.Name='Test Account' ; lstAccount.add(testAccount); Account testAccount1 = new Account(); testAccount1.Name='Test Account11' ; lstAccount.add(testAccount1); insert lstAccount; Test.startTest(); Test.setCurrentPage(Page.YOUR_PAGE); ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(lstAccount); stdSetController.setSelected(lstAccount); YOUR_Extension ext = new YOUR_Extension(stdSetController); Test.stopTest(); } }
January 02, 2021 at 12:03PM
Click here for more details...
=============================
The original post is available in Jayakrishna Ganjikunta by jayakrishnasfdc
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment