2

Can anyone tell me how to write the test class for pagination?
Below is my test class which covers 85%, but it doesn't cover the hasNext, hasPrevious and pageNumber methods.

Test Class

@isTest(SeeAllData=true)  
public class PaginationControllerTest {  

        public static testMethod void testSearchAccount() {  
        PageReference pageRef = Page.TestPagination;  
        Test.setCurrentPage(pageRef);

        // Instantiate a new controller with all parameters in the page

        PaginationController  p=new PaginationController ();

        p.getAccountList();
        p.Next();
        p.First();
        p.Last();
        p.Previous();
        p.Cancel();
        p.con.getHasPrevious();
        p.con.getHasNext();
        p.con.getPageNumber();


     }

}

Controller:

public class PaginationController {    
    public ApexPages.StandardSetController con {   
        get {    
                if(con == null) {    
                    con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name,Type FROM Account]));     
                    con.setPageSize(5);                         
                }   
                return con;   
        }   
        set;   
    }  

    public List<Account> getAccountList(){   
        return (List<Account>)con.getRecords();   
    }   

    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 first() {
        con.first();
    }

    public void last() {
        con.last();
    }

    public void previous() {
        con.previous();
    }

    public void next() {
        con.next();
    }

    public void cancel() {
        con.cancel();
    }        

}
2
  • Try creating more than 5 Account records in your test class.
    – Girbot
    Dec 9 '14 at 13:29
  • @Girbot It will give same Dec 9 '14 at 13:46
4

Here is a example I have handy:

//Instantiate the StandardSetController  
    public ApexPages.StandardSetController con{get; set;}


//Boolean to check if there are more records after the present displaying records  
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();
        }  
        set;  
    }  

//Boolean to check if there are more records before the present displaying records  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();
        }  
        set;  
    }  

//Page number of the current displaying records  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  

These lines can all be covered by simply calling them directly in your test method:

boolean test1 = controller.hasNext;
boolean test2 = controller.hasPrevious;
integer test3 = controller.pageNumber;

To clarify my create more than 5 Accounts comment - this is how I would actually test it. The above is purely for line coverage. So for example create 10 Accounts in your test method, instantiate the page/controller than assert the value of controller.hasNext is True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.