如何让mockito从列表中为for循环的每次迭代返回值 [英] How to make mockito return values from a list for each iteration on a for loop

查看:50
本文介绍了如何让mockito从列表中为for循环的每次迭代返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这个 for 循环:

Supose this for loop:

for (int i = 0; i < 10; i++) {
    long numFound = barObj.getNumFound();
    total += numFound;
}

我想让 getNumFound() 使用 mockito 为循环中的每次迭代从列表中返回值.我怎么做?

I want to make getNumFound() return values from a list for each iteration on the loop using mockito. How do I do that?

推荐答案

我会写以下内容:

Mockito.doAnswer(AdditionalAnswers.returnsElementsOf(myList))
       .when(myMock).myMethod(... whatever ...);

这使用了 Answer 描述的 此处 一个接一个地返回列表中的元素.

This uses the Answer described here to return elements of the list, one after the other.

这篇关于如何让mockito从列表中为for循环的每次迭代返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆