function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kunal Purohit 4Kunal Purohit 4 

How to write Test class for custom controller?

I am havng following Custom Controller class. How to write test Class for follwing code:
 
public with sharing class JournalPage1 {
public integer limitvalue=5;
public integer offsetvalue=0;
public integer papersize=0;
 public Date endDate { get; set; }
         public Date startDate { get; set; }
        public List<Author_Research_Paper__c> AuthList { get; set; }
    public boolean Showdetailsflag { get; set; }
    public List<wrapJournalPage1> WrapList { get; set; }
    public PageReference lastPage() {
    offsetValue = papersize - limitValue;
    callQuery();
            return null;
    }
    public PageReference nextPage() {
  offsetValue = offsetValue + LimitValue;
    callQuery();
        return null;
    }


    public PageReference previousPage() {
   i

f(offsetValue > 0){
    offsetValue = offsetValue - LimitValue;
    callQuery();
    }
        return null;
    }

    public PageReference firstPage() {
    offsetValue = 0;
    callQuery();
        return null;
    }
 public void callQuery(){
 WrapList = new List<wrapJournalPage1>();
// list<Research_Paper__c> rlist=[Select id from Research_Paper__c];
  papersize=count;
  for(Research_Paper__c rc:[Select Id,Name, Paper_Title__c,Name_of_Primary_Author__c,Status__c,Publication_Date__c from Research_Paper__c  limit:limitvalue OFFSET:offsetvalue]){
               wrapJournalPage1 obj=new wrapJournalPage1(rc);
                            Wraplist.add(obj); 
                            }                          
 }

    public void Showdetails() {
         List<Id> rpID = new List<Id>();
         for(wrapJournalPage1 wp : wrapList)
         {
         if(wp.flagWrap==true)
         {
         rpID.add(wp.rcWrap.Id);
         }
         }
         for(Id i : rpID)
         {
     AuthList=[Select Id, Research_Paper__r.Name, Research_Paper__r.Paper_Title__c, Author__r.Name,
                      Research_Paper__r.Status__c, Research_Paper__r.Submission_Date__c
                       From Author_Research_Paper__c
                       Where Research_Paper__c =: i];
         }
         
         Showdetailsflag=true;
    }
     public void updated() {
        Research=[Select id from Research_Paper__c];
                                
    count=Research.size();
    }

    public Integer count { get; set; }

    public List<Research_Paper__c> Research { get; set; }
    
    public JournalPage1()
{
Research=new list<Research_Paper__c>();

}
   public PageReference Show() {
         WrapList = new List<wrapJournalPage1>();
    
    if(startDate!=null && endDate!=null)
    {
         Research=[Select Id,Name, Paper_Title__c,Name_of_Primary_Author__c,Status__c,Publication_Date__c from Research_Paper__c where Publication_Date__c>=:startDate AND
                                         Publication_Date__c<=:endDate limit:limitvalue OFFSET:offsetvalue];
                            count=Research.size();
                            for(Research_Paper__c rc:Research)
                            {
                            wrapJournalPage1 obj=new wrapJournalPage1(rc);
                            Wraplist.add(obj);
                            }
    }
    
        return null;
    }


    
    
    Public class wrapJournalPage1{
    
    Public Research_Paper__c rcWrap{get; set;}
    public boolean flagWrap{get;set;}
    
    public wrapJournalPage1(Research_Paper__c rc){
            this.rcWrap=rc;
            this.flagWrap=false;
    }
    
    }
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Kunal,

Below is an example of test class for a custom controller, can you try checking it once:

>> https://developer.salesforce.com/forums/?id=906F00000005JRuIAM

Let me know if it helps and if this came in handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej