Java 使用 Mockito 验证 void 方法调用 n 次

2022-01-14 00:00:00 unit-testing mockito verify java junit

我正在尝试验证是否在 DAO 内部调用了一个 (void) 方法 - 我正在使用一个提交点,该提交点发送到该点的结果列表,重置列表并继续.假设我在列表中有 4 件事并且我的提交点为 1,我希望发送"方法被调用 4 次.我可以通过编写验证该方法是否被调用一次

I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues. Say I have 4 things in the list and I have a commit point of 1, I would expect the "send" method to be called 4 times. I can verify that the method gets called once by writing

Mockito.verify(mock).send()

它通过了.. 但我想验证它被调用的次数.我会认为

it passes.. but I want to verify the number of times it was called. I would think that

Mockito.verify(mock.send(), times(4))

就足够了,但它说参数不正确,无法验证.

would be sufficient, but it says the parameters are not correct for verify.

顺便说一句,如果我将 Mockito.verify(mock).send() 更改为 Mockito.verify(mock.send())Mockito.verify((mock).send()) 我得到同样的错误.对此有何想法?

Incidentally, if I change Mockito.verify(mock).send() to Mockito.verify(mock.send()) or Mockito.verify((mock).send()) I get the same error. Thoughts on this?

推荐答案

必要的方法是Mockito#verify:

public static <T> T verify(T mock,
                           VerificationMode mode)

mock 是您的模拟对象,mode 是描述应该如何验证模拟的 VerificationMode.可能的模式是:

mock is your mocked object and mode is the VerificationMode that describes how the mock should be verified. Possible modes are:

verify(mock, times(5)).someMethod("was called five times");
verify(mock, never()).someMethod("was never called");
verify(mock, atLeastOnce()).someMethod("was called at least once");
verify(mock, atLeast(2)).someMethod("was called at least twice");
verify(mock, atMost(3)).someMethod("was called at most 3 times");
verify(mock, atLeast(0)).someMethod("was called any number of times"); // useful with captors
verify(mock, only()).someMethod("no other method has been called on the mock");

您将需要来自 的这些静态导入Mockito 类,以便使用 verify 方法和这些验证模式:

You'll need these static imports from the Mockito class in order to use the verify method and these verification modes:

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

所以在你的情况下,正确的语法是:

So in your case the correct syntax will be:

Mockito.verify(mock, times(4)).send()

这将验证方法 send 在模拟对象上被调用 4 次.如果调用少于或多于 4 次,它将失败.

This verifies that the method send was called 4 times on the mocked object. It will fail if it was called less or more than 4 times.

如果你只是想检查,如果方法被调用过一次,那么你不需要传递一个VerificationMode.一个简单的

If you just want to check, if the method has been called once, then you don't need to pass a VerificationMode. A simple

verify(mock).someMethod("was called once");

就够了.它在内部使用 verify(mock, ti​​mes(1)).someMethod("was called once");.

would be enough. It internally uses verify(mock, times(1)).someMethod("was called once");.

可以在同一个 mock 上进行多个验证调用以实现介于"验证.Mockito 不支持这样的 verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");,但我们可以写

It is possible to have multiple verification calls on the same mock to achieve a "between" verification. Mockito doesn't support something like this verify(mock, between(4,6)).someMethod("was called between 4 and 6 times");, but we can write

verify(mock, atLeast(4)).someMethod("was called at least four times ...");
verify(mock, atMost(6)).someMethod("... and not more than six times");

相反,获得相同的行为.边界被包含,因此当方法被调用 4、5 或 6 次时,测试用例为绿色.

instead, to get the same behaviour. The bounds are included, so the test case is green when the method was called 4, 5 or 6 times.

相关文章