我想这样做:
verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class));
其中 argument1 是类型 Argument1 的特定实例,argument2 是类型 Argument2 的任何实例。
但是我得到一个错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded. 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"));
按照该建议,我可以编写以下内容,一切都很好:
verify(function, Mockito.times(1)).doSomething(Matchers.any(Argument1.class), Matchers.any(Argument2.class));
我在哪里寻找Argument1 类型的任何论证和Argument2 类型的任何论证。
我怎样才能实现这种期望的行为?
最佳答案
有不止一种可能的参数匹配器,其中一种是 eq
,它在异常消息中被提及。使用:
verify(function, times(1)).doSomething(eq(arg1), any(Argument2.class));
(应该有静态导入——eq()
是 Matchers.eq()
)。
您还有 same()
(它确实引用相等性,即 ==
),更一般地说,您可以编写自己的匹配器。
关于java - Mockito Matchers.any(...) 仅在一个参数上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897866/