Java 在 mockito 的 thenReturn 中枚举列表

Java Enumerating list in mockito's thenReturn

有没有办法在 mockito 的 thenReturn 函数中枚举列表中的项目,所以我 return 列表中的每个项目。到目前为止,我已经这样做了:

List<Foo> returns = new ArrayList<Foo>();
//populate returns list

Mockito.when( /* some function is called */ ).thenReturn(returns.get(0), returns.get(1), returns.get(2), returns.get(3));

这完全符合我的要求。每次调用该函数时,它 return 都是列表中的一个不同对象,例如 get(1)get(2)

但我想简化它并使其对任何大小的列表都更加动态,以防我有一个大小为 100 的列表。我试过这样的事情:

Mockito.when( /* some function is called */ ).thenReturn(
    for(Foo foo : returns) {
        return foo;
    }
);

我也试过这个:

Mockito.when(service.findFinancialInstrumentById(eq(1L))).thenReturn(
    for (int i=0; i<returns.size(); i++) {
        returns.get(i);
    }
);

但这不起作用....所以我如何在 thenReturn 中枚举此列表...我遇到了其他方法来喜欢 thenanswer但我不确定在这种情况下哪个最有效。

thenReturn() 方法签名是

thenReturn(T value, T... values)

所以它需要一个 T 的实例,后跟一个可变参数 T...,它是数组的语法糖。所以你可以使用

when(foo.bar()).thenReturn(list.get(0), 
                           list.subList(1, list.size()).toArray(new Foo[]{}));

但更简洁的解决方案是创建一个 Answer 实现,它以 List 作为参数,并在每次使用时回答列表的下一个元素。然后使用

when(foo.bar()).thenAnswer(new SequenceAnswer<>(list));

例如:

private static class SequenceAnswer<T> implements Answer<T> {

    private Iterator<T> resultIterator;

    // the last element is always returned once the iterator is exhausted, as with thenReturn()
    private T last;

    public SequenceAnswer(List<T> results) {
        this.resultIterator = results.iterator();
        this.last = results.get(results.size() - 1);
    }

    @Override
    public T answer(InvocationOnMock invocation) throws Throwable {
        if (resultIterator.hasNext()) {
            return resultIterator.next();
        }
        return last;
    }
}

另一种方法(但就个人而言,我更喜欢 JB Nizet SequenceAnswer 的想法),就像这样...

OngoingStubbing stubbing = Mockito.when(...);
for(Object obj : list) {
    stubbing = stubbing.thenReturn(obj);
}

我知道原来的问题特别要求使用 .thenReturn(),但如果你可以使用 .thenAnswer(),mockito 有一个 AdditionalAnswers.returnsElementsOf()

在此主题中找到答案