0

For my test I need to mock the hasNext() method of my cursos. However to fully test my code i need 250 iterations to send to a bulkRequest. Therefore i need 250 x true and 1 x false at the end.

I created a boolean array filled with 250 true's and 1 false

what i got

@Mock
private Cursor<Record> cursor;

public void myTest(){
  when(cursor.hasNext()).thenReturn(true, false);
}

but now I need 250 conditions for cursor so i created a boolean Array but obvoiusly it doesn't compile

final boolean[] cursorsResponses = fillCursors();
when(cursor.hasNext()).thenReturn(cursorsResponses);
Maevy
  • 261
  • 4
  • 24
  • 1
    Create a class that has a `hasNext()` method and all the internal state/logic you want, then call it at `thenReturn`. – m0skit0 Jan 28 '19 at 11:39

1 Answers1

1

So in your case:

when(cursor.hasNext()).thenAnswer(new Answer() {
   private int count = 0;

   public Object answer(InvocationOnMock invocation) {
        return (count++ < 250);
   }
});
xerx593
  • 12,237
  • 5
  • 33
  • 64