如何模拟具有高回报的hasNext

时间:2019-01-28 11:37:43

标签: java junit mockito

对于我的测试,我需要模拟cursos的hasNext()方法。但是,要完全测试我的代码,我需要进行250次迭代才能发送到bulkRequest。因此,最后我需要250 x true和1 x false。

我创建了一个布尔数组,其中填充了250个真值和1个假值

我得到了什么

@Mock
private Cursor<Record> cursor;

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

但是现在我需要250个条件作为游标 所以我创建了一个布尔数组,但是很奇怪,它不能编译

final boolean[] cursorsResponses = fillCursors();
when(cursor.hasNext()).thenReturn(cursorsResponses);

1 个答案:

答案 0 :(得分:1)

所以在您的情况下:

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

   public Object answer(InvocationOnMock invocation) {
        return (count++ < 250);
   }
});