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
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!!!
SubratSubrat (Salesforce Developers) 
Hello ,

Try with this test class :
 
@isTest
public class CustomIterableTest {

    @isTest
    static void testCustomIterable() {
        // create test data
        List<AccountWrapper> accountWrappers = new List<AccountWrapper>();
        for (Integer i = 0; i < 20; i++) {
            accountWrappers.add(new AccountWrapper(new Account(Name = 'Test Account ' + i)));
        }
        
        // instantiate class
        CustomIterable customIterable = new CustomIterable(accountWrappers);
        
        // test hasNext and next methods
        System.assert(customIterable.hasNext());
        List<AccountWrapper> nextList = customIterable.next();
        System.assertEquals(10, nextList.size());
        System.assert(!customIterable.hasNext());
        
        // test hasPrevious and previous methods
        System.assert(customIterable.hasPrevious());
        List<AccountWrapper> previousList = customIterable.previous();
        System.assertEquals(10, previousList.size());
        System.assert(customIterable.hasNext());
    }
}
Hope it helps !
Thank you.