You need to sign in to do that
Don't have an account?
Gaurav 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:
Test Class is as follows:
Code is not covered for
and
Please suggest.
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.
Please try below test class. I hope that will help you
Please let us know if this will help you
Thanks
Amit Chaudhary
All Answers
Thanks
Shaijan Thomas
This should always work !
Please try below test class. I hope that will help you
Please let us know if this will help you
Thanks
Amit Chaudhary
Thanks for your help.
-Gaurav