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
Egor PalatovEgor Palatov 

Test class for pagination(with Custom settings)

Hi, can somebody advice how to create test class for this pagination?

Controller:

public class ParseCSVController {
    
	public Blob csvFileBody{get;set;}
	public string csvAsString{get;set;}
	public String[] csvFileLines{get;set;}
	public List<Car_detail__c> DetailList{get;set;}
    public List<SelectOption> paginationSizeOptions{get;set;}
    public List<Car_Detail__c> showNow{get;set;}
	public List<NumRecOnPage__c> PageSize{get;set;} 
	public integer ResultSize{get;set;} 
	public integer PageNumber{get;set;} 
	public integer limitSize{get;set;}
    public Integer counter = 0;
	
public ParseCSVController(){

	csvFileLines = new String[]{};
    showNow = new List<Car_Detail__c>();
	DetailList = New List<Car_detail__c>();
        PageSize = NumRecOnPage__c.getAll().values(); 
        limitSize = PageSize[0].Number_Records__c.intValue() ;
        ResultSize = 0;
        PageNumber = counter;
  }
    
    
    public boolean getHasNext(){
        if(PageNumber<getNumPages())
            return true;
        return false;
    }
    public boolean getHasPrevious(){
        if(PageNumber>1)
            return true;
        return false;
    }
    public void Next(){
        showNow.clear();
        counter+=1;
        PageNumber += 1;
        
        integer i0 = counter * limitSize;

        for(integer i = i0 ;i<ResultSize;i++){
            if(i<(i0+limitSize)){
                showNow.add(DetailList.get(i));
            }else break;
        }
    }
    public void Previous(){
        showNow.clear();
        PageNumber = PageNumber-1;
        counter=counter-1;
        
        integer i0 = counter * limitSize;
        
        for(integer i = i0 ;i<ResultSize;i++){
            if(i<(i0+limitSize)){
                showNow.add(DetailList.get(i));
            }else break;
        }
    }
    public void reload(){
        showNow.clear();
        PageNumber = 1;
        counter = 0;
        integer i0 = counter * limitSize;
        for(integer i=i0;i<ResultSize;i++){
            if(i<i0+limitSize){
                showNow.add(DetailList.get(i));
            }else break;
        }  
    }
    public integer getNumPages(){
        integer ost = math.mod(ResultSize, limitSize);
        if(ost>0)
            return ResultSize/limitSize+1;
        return ResultSize/limitSize;
    }
    public void First(){
        counter = 0;
        PageNumber = 1;
        showNow.clear();
        for(integer i=0;i<ResultSize;i++){
            if(i<limitSize){
                showNow.add(DetailList.get(i));
            }else break;
        }  
    }
public void Last(){
	counter = getNumPages()-1;
	PageNumber = getNumPages();
	showNow.clear();
        
	integer i0 = counter*limitSize;
	for(integer i=i0;i<ResultSize;i++){
		if(i<(i0+limitSize)){
			showNow.add(DetailList.get(i));
            }else break;
        }  
    }
public integer getCounterValue(){
        return counter;
    }

Test class(that i have ATM):
@isTest
public class ParseCSVControllerTest {
static testMethod void getCustomSettings(){ 
	NumRecOnPage__c customSet = new NumRecOnPage__c(); 
	customSet.Name = '10'; 
	customSet.Number_Records__c = 10; 
	insert customSet; 

	Car__c[] TestCars = new Car__c[]{new Car__c(Name = 'Mersedes'), new Car__c(Name = 'Audi'), 
	new Car__c(Name = 'BMW')}; 

	insert TestCars; 
}  
}

 
Mukesh_SfdcDevMukesh_SfdcDev
Hi Egor,

In the test class after the insert operation, you should create a class instance and then assign the value in property of class
after that call the apex class method by changing the property value.



1. Custom Settings are not visible in the Test Class by default.
2. Method-1 : You can use @istest(SeeAllData=true)
3. Method2-: You can create custom setting required for the test in the test method
4.Note that if you create custom setting through code within test method then you will not be able to use (SeeAllData=true)

create custom setting like the below code
CustomSetting__c cs = new CustomSetting__c();
        cs.Name='test';
        //cs.Other fiels values
     insert cs;


Thanks
Mukesh Kumar
     
Egor PalatovEgor Palatov

Hi, Mukesh. Thanks for answer.

yes i created my custom setting, but i dunno how to test my pagination methods