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
Ashok S 7Ashok S 7 

how can i cover this in the test class

here i send my code 
controller
-----------------
public with sharing class CustomPaginationController1 
{
    public Account acc {get;set;}   
    public ApexPages.StandardSetController con{get; set;} 
    public CustomPaginationController1 ()
    {
       acc = new Account();
       lstAccount = new List<Account>();
    }
    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  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
        else
        {
        }
       return null;
    }
    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
-------------------------------
@isTest(seeAllData=true)
public class CustomPaginationController1_Test
{
    public static testmethod void test()
    {
       test.startTest();
        //inserting a record into Account object
        Account acc = new Account();
        acc.Name = 'Sample Test';
        acc.Phone = '8019982366';
        insert acc;
   
       //using pagereference inthe test class
       PageReference pg = page.Scenario_Vf_Page;
       pg.getparameters().put('id',string.valueOf(acc.id));
       test.setcurrentpage(pg);
       
       //calling the controller inthe test class
       CustomPaginationController1 cpc = new CustomPaginationController1();
       //cpc.lstAccount ();
       cpc.Search();
       //cpc.hasNext();
       test.stopTest();
     }
}

here how can i cover bold lines in the test class please help me any one
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. Note always try to above seeAllData in test class
@isTest()
public class CustomPaginationController1_Test
{
    public static testmethod void test()
    {
        //inserting a record into Account object
        Account acc = new Account();
        acc.Name = 'Sample Test';
        acc.Phone = '8019982366';
        insert acc;

		test.startTest();
   
			PageReference pg = page.Scenario_Vf_Page;
			pg.getparameters().put('id',string.valueOf(acc.id));
			test.setcurrentpage(pg);

			CustomPaginationController1 cpc = new CustomPaginationController1();
			cpc.acc.Name ='Sample Test';
			cpc.acc.Phone ='8019982366';
			cpc.Search();

			List<Account> lstAccountTest = cpc.lstAccount;
			Boolean bhasNext = cpc.hasNext;
			Boolean bhasPrevious = cpc.hasPrevious;
			
        test.stopTest();
     }
}

Please mark this as solution if this will help you. So that if some one has same issue this post help other.

Thanks
Amit Chaudhary
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Ashok,
pls see below code it will work fine

@isTest()

public class CustomPaginationController1_Test {
  public static testmethod void test(){

//inserting a record into Account object
Account acc = new Account();
acc.Name = 'Sample Test';
acc.Phone = '8019982366';
insert acc;
test.startTest();
PageReference pg = page.Scenario_Vf_Page;
pg.getparameters().put('id',string.valueOf(acc.id));
test.setcurrentpage(pg);


CustomPaginationController1 cp=new CustomPaginationController1();
cp.search();
cp.previous();
cp.next();
List<Account>alist=cp.lstAccount ;
Boolean bp=cp.haspervious;
Boolean bn=cp.hasnext;
Integer in=cp.pagenumber;
 test.stopTest();
}
}

If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.

Regards
Eswar Prasad