我正在寻找官方提供的方法来替换最后一行...
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
// ...
Runnable mock = mock(Runnable.class);
then(mock).should(times(1)).run();
...更短的内容,例如:
then(mock).should(once()).callMethod();
为什么更短?因为 IntelliJ 通常有用的功能会显示参数的名称,从而导致许多长代码行:
then(mock).should(times(wantedNumberOfInvocations: 1)).callMethod();
我知道我可以轻松编写这样的函数,但随后我要么必须为几个工作区项目复制它,要么引入一个新模块,这对我来说似乎有点过头了。
如果您能为 Mockito.atLeast(1)
指出类似 Mockito.atLeastOnce()
的内容,只是没有 atLeast
,那就太好了> 部分:-) 谢谢
最佳答案
另一种选择是使用简单的 should()
,它是单次调用的别名:
then(mock).should().callMethod();
关于java - org.mockito.Mockito.times(1) 有方便的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52743843/