+ Start a Discussion
Shruthi GM 4Shruthi GM 4 

Test class for Customiterable class

/* class */
global class  CustomIterable implements Iterator<list<AccountWrapper>>
{
list<AccountWrapper> InnerList{get; set;}
list<AccountWrapper> ListRequested{get; set;}
Integer i {get; set;}
public Integer setPageSize {get; set;}
public CustomIterable(List<AccountWrapper> lstAccWr)
{
InnerList = new list<AccountWrapper>();
ListRequested = new list<AccountWrapper>();    
InnerList = lstAccWr;
setPageSize = 10;
i = 0;
}  

 
global boolean hasNext(){
if(i >= InnerList.size()) {
return false;

       } else {

           return true;

       }

   }

    

global boolean hasPrevious(){
system.debug('I am in hasPrevious' + i);
if(i <= setPageSize) {
return false;
} else {
return true;
}
}  

 
global list<AccountWrapper> next(){      
system.debug('i value is ' + i);
ListRequested = new list<AccountWrapper>();
integer startNumber;
integer size = InnerList.size();
if(hasNext())

if(size <= (i + setPageSize))
{
startNumber = i;
i = size;
}
else
{
i = (i + setPageSize);
startNumber = (i - setPageSize);
}
system.debug('i value is =====' + i);
system.debug('i value is 2==== ' + (i - setPageSize));

for(integer start = startNumber; start < i; start++)
{
ListRequested.add(InnerList[start]);
}

}
return ListRequested;
}

    
global list<AccountWrapper> previous(){     
ListRequested = new list<AccountWrapper>();
system.debug('i value is previous before =====' + i);
integer size = InnerList.size();
if(i == size)
{
if(math.mod(size, setPageSize) > 0)
{   
i = size - math.mod(size, setPageSize);
}
else
{
i = (size - setPageSize);
}
}
else
{
i = (i - setPageSize);
}
system.debug('i value is previous =====' + i);
system.debug('i value is 2previous ==== ' + (i - setPageSize));

for(integer start = (i - setPageSize); start < i; ++start)
{
ListRequested.add(InnerList[start]);
}
return ListRequested;

   }  


}

How to write test class for this particular class?

I have written it in the following way:-

@istest

public class CustomIterabletest{


public static testmethod void testvalidate(){

list<AccountWrapper> InnerList;
list<AccountWrapper> ListRequested;
List<AccountWrapper> lstAccWr;

InnerList = new list<AccountWrapper>();
ListRequested = new list<AccountWrapper>();    
InnerList = lstAccWr;
integer setPageSize = 10;
integer i = 0;



test.starttest();
CustomIterable custom=new CustomIterable(lstAccWr);
custom.hasNext();
custom.hasPrevious();
custom.next();
custom.previous();
test.stoptest();
}
}

But it is not even covering the single method!!!