+ Start a Discussion
thomsantthomsant 

Controller

Hi All,

Need urgent help in writing a test class for the below controller  , not sure how i can do it .

Help really appreciated

 

Regards

Tom

 

 

public without sharing class RelatedListController{

    private list<sObject> mSObjects;
    public list<sObject> SObjects{
        get{return mSObjects;}
        set{
            numObjects = value==null||value.isEmpty() ? 0 : value.size();
            DescribeSObjectResult res = value.getsObjectType().getDescribe();
            isDeletable = res.isDeletable();
            isCreateable = res.isCreateable();
            isUpdateable = res.isUpdateable();
            defaultTitle = res.getLabelPlural();
            isActivity = res.getSObjectType()==Task.SObjectType||res.getSObjectType()==Event.SObjectType;
            map<String,Schema.SObjectField> fieldMap = res.fields.getMap();
            DescribeFieldResult fieldRes = isActivity ? fieldMap.get('Subject').getDescribe() : fieldMap.get('Name').getDescribe();
            nameLabel = fieldRes.getLabel();
            nameField = fieldRes.getName();
            keyPrefix = res.getKeyPrefix();
            mSObjects = value;
        }
    }
        
    private String defaultTitle;
    private String overrideTitle;
    
    public String parameters {
        get{return parameters==null ? '' : parameters;}
        set;}
    
    public String PageTitle{
        get{return overrideTitle==null||overrideTitle=='' ? defaultTitle : overrideTitle;}
        set{overrideTitle = value;}}
    
    public String buttonLabel{
        get{return overrideButton==null||overrideButton=='' ? String.format(System.Label.New_Button,new list<String>{PageTitle}) : overrideButton;}
        set{overrideButton = value;}}
    
    private String overrideButton;
    
    private integer numObjects;
    public integer rowIncrement{
        get{return numObjects-mRows<mRows ? numObjects-mRows : mRows;}}
    
    private integer mRows;
    public integer rows{
        get{
            if(mRows==null||mRows==0){
                mRows = numObjects<5 ? numObjects : 5;
            }
            return mRows;
        }
    }
    
    public String retURL{
        get{return retURL==null ? '' : 'retURL=' + retURL;}
        set;}
    
    public boolean noRecords{
        get{return numObjects==0;}}
    
    public boolean moreRows{
        get{return mRows<numObjects;}}
    
    public String keyPrefix {get;private set;}
    public String nameLabel {get;private set;}
    public String nameField {get;private set;}
    public boolean isActivity{get;private set;}
    public boolean isUpdateable{get;private set;}
    public boolean isDeletable{get;private set;}
    public boolean isCreateable{get;private set;}
    public ApexPages.PageReference newRecord(){
        return new ApexPages.PageReference('/' + keyprefix + '/e' + parameters);}
    
    public ApexPages.PageReference incrementRows(){
        mRows = mRows + rowIncrement;
        return null;}
}

Ronak PatelRonak Patel

Just call this both methods from test class and get all values of all Variables

 

1. public ApexPages.PageReference newRecord()

2. public ApexPages.PageReference incrementRows()

 

test class

RelatedListController test =new RelatedListController();

list<sObject> SObjects1=test.SObjects1();

// Like this call all variables 

//and test calss 100%

PageReference  p=test.newRecord();

PageReference  p1=test.incrementRows();