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
Gaurav AgnihotriGaurav Agnihotri 

Test Class for pagination

I am able to get 80% code coverage, but it didnt covers the hasnext,previous and pagenumber method. 
Below is the class:
public with sharing class CustomPaginationController 
{

    public CustomPaginationController(ApexPages.StandardController controller) 
    {
       acc = new Account();
       lstAccount = new List<Account>();

   }

    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
  /*  public CustomPaginationController ()
    {

    }*/
    public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }  
        set;
    }  
    public PageReference Search()
    {
        String query= '';
        String strFilter = '';
      
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
            strFilter  = strFilter  +  ' And Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
         {
        	strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
         }
       if(acc.ShippingCity != null && (acc.ShippingCity).trim() !='' )
        {
        	strFilter  = strFilter  +  ' And ShippingCity like \''+acc.ShippingCity+'%\'' ;
        }
        if(acc.ShippingCountry != null && (acc.ShippingCountry).trim() !='' )
        {
            strFilter  = strFilter  +  ' And ShippingCountry like \''+acc.ShippingCountry+'%\'' ;
        }
        if(acc.Account_Type__c != null && (acc.Account_Type__c).trim() !='' )
        {
            strFilter  = strFilter  +  ' And Account_Type__c like \''+acc.Account_Type__c+'%\'' ;
        }
        if(acc.Customer_Number__c != null && (acc.Customer_Number__c).trim() !='' )
        {
            strFilter  = strFilter  +  ' And Customer_Number__c like \''+acc.Customer_Number__c+'%\'' ;
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone,ShippingCity,ShippingCountry,Account_Type__c,Customer_Number__c from Account  where name != null '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(20);
        }
        else
        {
        }
       return null;
    }
    public PageReference createAccount(){
            PageReference newAccPage2 = new PageReference('/001/e');
            // do not longer override the creation action   
            newAccPage2.getParameters().put('nooverride', '1');
            newAccPage2.getParameters().put('acc2', acc.Name);
            newAccPage2.getParameters().put('acc10', acc.Phone);
            newAccPage2.setRedirect(true);
            return newAccPage2; 

   }
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
    public void previous()  
    {  
        con.previous();  
    }  
    public void next()  
    {  
        con.next();  
    }  
   
}

Test Class is as follows:
@isTest
private class TestCustomPaginationController {
static testMethod void TestPaginationAccount() 
    {
		//creating an Account
		Account Acc=new Account();
		Acc.Name='Test Account';
		Acc.phone='5591231234';
        Acc.Currency_Number__c='PROSVAA';
		Acc.ShippingCity='Los Angles';
		Acc.Account_Type__c= 'Integrator';
		Acc.ShippingCountry='United States';
		Insert Acc;		
		//PageReference pageRef = Page.newAccPage2;
		//Test.setCurrentPage(pageRef);
		ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(Acc);
		ApexPages.currentPage().getParameters().put('Id',acc.id);
		CustomPaginationController ec = new CustomPaginationController(sc);
        ec.acc.name='Test Account';
        ec.acc.phone='5591231234';
        ec.acc.ShippingCity='Los Angles';
        ec.acc.Account_Type__c='Integrator';
        ec.acc.Customer_Number__c='PROSVAA';
        ec.acc.ShippingCountry='United States';
        ec.Search();
        List<Account> lstAccount =new list<Account>();
        lstAccount.add(ec.acc);
        ec.CreateAccount();
        PageReference pageRef = Page.SearchandCreateAccount;
        Test.setCurrentPage(pageRef);
        ec.next();
        ec.previous();
        //p.pageNumber();
        //p.hasNext();
        //p.hasPrevious();
        //ec.acc.hasNext();
        //CustomPaginationController NewPagination=CustomPaginationController(ec.acc);
        //NewPagination.hasNext();
        //NewPagination.hasPrevious();
        //acc.hasNext(); 
        //lstAccount.hasPrevious();
        //ec.getPageNumber();
        //ec.previous();
        //ec.next();
		//list<account> Accs=ec.Acc;
		//System.assertEquals(1, Accs.size());
	}
}

Code is not covered for 
public List<Account> lstAccount 
    {  
        get  
        {  
            if(con != null)  
                return (List<Account>)con.getRecords();  
            else  
                return null ;  
        }

and 
public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }

Please suggest.
Best Answer chosen by Gaurav Agnihotri
Amit Chaudhary 8Amit Chaudhary 8
Hi ,
Please try below test class. I hope that will help you
@isTest
private class TestCustomPaginationController {
static testMethod void TestPaginationAccount() 
    {
        //creating an Account
        Account Acc=new Account();
        Acc.Name='Test Account';
        Acc.phone='5591231234';
        Acc.ShippingCity='Los Angles';
        Acc.ShippingCountry='United States';
        Insert Acc;     
        
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(Acc);
        ApexPages.currentPage().getParameters().put('Id',acc.id);
        CustomPaginationController ec = new CustomPaginationController(sc);

        ec.acc.name='Test Account';
        ec.acc.phone='5591231234';
        ec.acc.ShippingCity='Los Angles';
        ec.acc.ShippingCountry='United States';
        List<Account> lstAccountTemp = ec.lstAccount;
        ec.Search();

        List<Account> lstAccountTemp1 = ec.lstAccount;
        Boolean nxt = ec.hasNext ;
        Boolean prev = ec.hasPrevious  ;
        Integer i = ec.pageNumber  ;
        ec.previous();
        ec.next();
        

        List<Account> lstAccount =new list<Account>();
        lstAccount.add(ec.acc);

    }
}

Please let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Shaijan ThomasShaijan Thomas
Instead of 1 Account record, can you try with 100 record, Next and previous work based on your page size. Looks like page size is 20.
Thanks
Shaijan Thomas
Sandeep Mishra 7Sandeep Mishra 7
You can always call these manually as below:
ec.hasnext();

This should always work !
Amit Chaudhary 8Amit Chaudhary 8
Hi ,
Please try below test class. I hope that will help you
@isTest
private class TestCustomPaginationController {
static testMethod void TestPaginationAccount() 
    {
        //creating an Account
        Account Acc=new Account();
        Acc.Name='Test Account';
        Acc.phone='5591231234';
        Acc.ShippingCity='Los Angles';
        Acc.ShippingCountry='United States';
        Insert Acc;     
        
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(Acc);
        ApexPages.currentPage().getParameters().put('Id',acc.id);
        CustomPaginationController ec = new CustomPaginationController(sc);

        ec.acc.name='Test Account';
        ec.acc.phone='5591231234';
        ec.acc.ShippingCity='Los Angles';
        ec.acc.ShippingCountry='United States';
        List<Account> lstAccountTemp = ec.lstAccount;
        ec.Search();

        List<Account> lstAccountTemp1 = ec.lstAccount;
        Boolean nxt = ec.hasNext ;
        Boolean prev = ec.hasPrevious  ;
        Integer i = ec.pageNumber  ;
        ec.previous();
        ec.next();
        

        List<Account> lstAccount =new list<Account>();
        lstAccount.add(ec.acc);

    }
}

Please let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please let us know if above test class resolved your issue or you need more help
Gaurav AgnihotriGaurav Agnihotri
The issue has been resolved.

Thanks for your help.

-Gaurav