gpt4 book ai didi

java - org.mockito.Mockito.times(1) 有方便的方法吗?

转载 作者:行者123 更新时间:2023-11-30 05:58:24 26 4
gpt4 key购买 nike

我正在寻找官方提供的方法来替换最后一行...

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com