Requirement : Write Test Class for Extension Controller.
Before going to code first we know about Extension Controller in Apex.
A controller extension is an Apex class that extends the functionality of a standard or custom controller. Use controller extensions when:
- You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.
- You want to add new actions.
- You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.
Controller E xtension Class: below is our Controller Extension Class
public class yourExtController { public Account acc {get;set;} public yourExtController (ApexPages.StandardController stdController){ this.acc = (Account)stdController.getRecord(); } }
Test Class : Now we write test class for Controller extension
@istest public class yourExtController_TC { static testMethod void test1() { Account acc = new Account(name='testAccount'); insert acc; Test.startTest(); ApexPages.StandardController account = new ApexPages.StandardController(acc); yourExtController obj = new yourExtController(account); obj.yourmethod(); Test.stopTest(); } }
Hits: 1401