Hi,
Throwing IndexOutOfBoundsException looks like a bug. We'll fix it.
However you have two workarounds,
First, you have to actually, change the reference of the dbServiceOngoingStubbing :
OngoingStubbing<List<SimpleDoc>> dbServiceOngoingStubbing =
when(service.get50DocsByUserNameOrIdFromDate(anyString(), eq("JUnitTest User 1"), eq("Z1234"), anyString()));
while(user1SampleDocs > 50){
dbServiceOngoingStubbing = dbServiceOngoingStubbing.thenReturn(sampleDocuments(50));
user1SampleDocs -= 50;
}
dbServiceOngoingStubbing.thenReturn(sampleDocuments(user1SampleDocs));
dbServiceOngoingStubbing.thenReturn(sampleDocuments(0));
Currently, what is important is to store the reference to the first stub.
The second way is to prepare the first answer, the other consecutive answers, then call thenReturn:
Something like (modulo some compiler warnings / error) :
user1SampleDocs -= 50; // start one page further
List<List<SimpleDoc>> otherPages = newArrayList();
while(user1SampleDocs > 50){
otherPages.add(sampleDocuments(50));
user1SampleDocs -= 50;
}
when(service.get50DocsByUserNameOrIdFromDate(anyString(), eq("JUnitTest User 1"), eq("Z1234"), anyString()))
.thenReturn(sampleDocument(50), otherPages.toArray())
.thenReturn(sampleDocuments(user1SampleDocs))
.thenReturn(sampleDocuments(0));
Hope that helps :)