You need to sign in to do that
Don't have an account?

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