Mockito验证方法称为忽略参数

时间:2018-06-07 04:55:48

标签: mockito

我想要验证的DAO方法在Service

中调用
send(User user, Properties prop)

我可以在服务中验证使用受保护的方法,但我认为它应该是私有的

verify(dao).send(user, service.getProp())

我试着以不同的方式定义接受任何属性:

verify(dao).send(user, any(Properties.class)); // or any()

verify(dao).send(user, Matchers.isA(Properties.class)));

但是所有参数都无效

FAILED: testService
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.package.TestService.testService(TestService.java:330)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

1 个答案:

答案 0 :(得分:3)

正如异常详细说明一样,您不允许将原始值(user)与匹配器(any(...))混合使用。

相反,使用eq(...)匹配器对所有参数使用匹配器:

verify(dao).send(eq(user), any());