Java验证void方法使用Mockito调用n次 [英] Java verify void method calls n times with Mockito

查看:1078
本文介绍了Java验证void方法使用Mockito调用n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证在DAO中调用(void)方法 - 我正在使用一个提交点,该提交点将结果列表发送到该点,重置列表并继续。
假设列表中有4个东西,我的提交点为1,我希望send方法被调用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(模拟).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 是您的模拟对象,模式 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 类,以便使用验证方法和这些验证模式:

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;

(班级 VerificationModeFactory 还提供上述验证因此你也可以将这个类用于静态导入。)

(The class VerificationModeFactory also provides the mentioned verification modes. So you could also use this class for the static imports.)

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

So in your case the correct syntax will be:

Mockito.verify(mock, times(4)).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,times(1))。someMethod(被调用一次);

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

这篇关于Java验证void方法使用Mockito调用n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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