+ Start a Discussion

Test class for VF page controller

Hello I have a simple controller for a visual force page and need to write the test methods , can someone help me out.


Here is the code for the controller 


public class EditOveride {
 public EditOveride() {}
    Id AccountId;
    public EditOveride(ApexPages.StandardController stdController) {
    // get id from URL
        AccountId = ApexPages.currentPage().getParameters().get('id');  
    public boolean displayPopup {get; set;}
     public void showPopup() {
        displayPopup = true;
    public  PageReference closePopup() {
            displayPopup = false;  

       return new PageReference('/' + AccountId);
    public PageReference editPage() {      
      return new PageReference('/'+AccountId+'/e?retURL=%2F'+AccountId+'&nooverride=1');                                                             


Unit tests work just the same for Controllers. Create your objects, create the controller and call the methods.


You don't need to do anything special.


One thing I have noticed however is it's much earlier to rewrite your constructor this way:


public EditOveride(ApexPages.StandardController stdController) {
    // get id from URL
        AccountId = ((Account) stdController.getRecord()).Id



You then don't need to mess around in the Unit test Constructor.

Just create a new Account e.g.


Account a = new Account(Name='My Test Account', ... );

insert a;

EditOveride eo = new EditOveride(new Apexpages.Standardcontroller(a));

Deepika GulatiDeepika Gulati



 i am Trying to Test the Controller by Passing the new ApexPages.standardContoller(task1); as the Paremeter to the Controller class constructor but it is giving me the error as


Error: Compile Error: Constructor not defined: [ParentChildActivity].<Constructor>(ApexPages.StandardController). 



The Code is given below:


Controller Code

public ParentChildActivity(ApexPages.StandardController stdController) 
        this.o= (Task)stdController.getRecord(); 
        t1=[Select Activity_TRN__c,Id,Parent_Activity_TRN__c from Task where id=:o.Id];
        System.debug('Task Id is '+ t1.Id);



Test Class

         Task task1=new Task(OwnerId = u.id, Subject = 'Demo1', Whoid = lead1.id, Description = 'Test Test', ActivityDate=system.today(), Due_Time__c = '9:00 AM', Customer_Promise__c = true, Customer_CIDN__c='1',Activity_TRN__c='10');           
         insert task1;

         ApexPages.StandardController sc=new ApexPages.StandardController(task1);
         ParentChildActivity pc=new ParentChildActivity(sc);