+ Start a Discussion
prati@salesforceprati@salesforce 

Test class for a custom controller

Hi,  I have written a vf page and a custom controller to use the page on my home page as a  home page component. So I am not really passing any ID to the vf page. It just uses a table to display some values on the home page as the current user. How do I write a test class for this? Please advice me.
public with sharing class FinancialCashAudit {
    public List<Cash_Audit__c> cashAudit{get; set;}
    public List<Cash_Audit__c> cashAuditlist;
    public Integer counter = 0;
    public Integer list_size = 5;
    public Integer total_size;
    private List<Id> ophier;
    public FinancialCashAudit(){
        ophier = new List<Id>();
        for(User_Access__c use : [select id, Operational_Hierarchy__c from User_Access__c where User__c = :UserInfo.getUserId() and active__c = true]){
            ophier.add(use.Operational_Hierarchy__c);
        }
        cashAudit = [select name, Operational_Hierarchy__c, Operational_Hierarchy__r.Name,
                    Submitted_By__c, Submitted_By__r.Name, Submitted_Date__c from Cash_Audit__c
                    where Operational_Hierarchy__c IN :ophier ];
        total_size  = cashAudit.size();
        
    }
    public List<Cash_Audit__c> getcashAuditlist(){
        cashAuditlist = [select name, Operational_Hierarchy__c, Operational_Hierarchy__r.Name,
                    Submitted_By__c, Submitted_By__r.Name, Submitted_Date__c from Cash_Audit__c
                    where Operational_Hierarchy__c IN :ophier ORDER BY Submitted_Date__c DESC  LIMIT: list_size OFFSET : counter];
        return cashAuditlist;
    }
    public PageReference Beginning(){
        counter = 0;
        System.debug('Counter is '+counter);
        getcashAuditlist();
        return null;
    }
    public PageReference Previous(){
        counter -= list_size;
        System.debug('Counter is '+counter);
        getcashAuditlist();
        return null;
    }
    public PageReference Next(){
        counter += list_size;
        System.debug('Counter is '+counter);
        getcashAuditlist();
        return null;
    }
    public pageReference End(){
        counter = total_size - math.mod(total_size, list_size);
        System.debug('Counter is '+counter);
        getcashAuditlist();
        return null;
    }
     public Boolean getDisablePrevious(){
        if(counter==0) return true;
        else return false;
    }
    public Boolean getDisableNext(){
        if((counter + list_size) > total_size) return true;
        else return false;
    }
    public Integer getPageNumber(){
        if(math.mod(counter, list_size)>0){
            return counter/list_size + 1;
        }else{
            return counter/list_size ;
        }
    }
    public Integer getTotalPages(){
        
            return (total_size/list_size);
        
    }
   
    
}
This controller displays all the cash audits with operational hierarchy. the way it is shared is there is a object called User Access which has fields User and  operational_hierarchy. Please give me some idea on how to start on this.
Thank you
 
Best Answer chosen by prati@salesforce
bob_buzzardbob_buzzard
You write a test class for this the same way that you would any other:

- Set up some data for the controller to access (in this case it looks like User_Access__c and Cash_Audit__c records)
- Instantiate the controller - as its custom you don't need to pass any parameters
- Execute a method/methods
- Verify the behaviour with asserts.

The fact that the class is used as a controller is fairly immaterial.

All Answers

bob_buzzardbob_buzzard
You write a test class for this the same way that you would any other:

- Set up some data for the controller to access (in this case it looks like User_Access__c and Cash_Audit__c records)
- Instantiate the controller - as its custom you don't need to pass any parameters
- Execute a method/methods
- Verify the behaviour with asserts.

The fact that the class is used as a controller is fairly immaterial.
This was selected as the best answer
prati@salesforceprati@salesforce
Thank you. I will try that.