[英]invalid matchers when mocking
I want to test this method : 我想测试这种方法:
public void some_method(SomeFactory someFactory) {
A a = someFactory.populateWithParameter(parameter1, parameter2, parameter3, parameter4);
a.method_call();
....
}
the factory goes this way 工厂就是这样
public class SomeFactory(){
// some constructor
public A populateWithParameter(SomeClass1 someClass1, SomeClass2 someClass2, String string, int parameter){
return new A(someClass1, someClass2, string, parameter)
}
}
and the test is 测试是
public void testSomeMethod() throws Exception {
SomeFactory someFactory = mock(SomeFactory.class);
when(someFactory.populateWithParameter(
any(SomeClass1.class), any(SomeClass2.class),
anyString(), anyInt())).thenReturn(notNull());
mainActivity.some_method(someFactory);
...
}
I get this message 我收到此消息
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
4 matchers expected, 1 recorded:
You aren't allowed to use notNull()
as a return value. 不允许使用notNull()
作为返回值。 Mockito matchers only stand in for arguments in calls to when
and verify
, and cannot work as return values. Mockito匹配器仅代表对when
和verify
调用中的参数,不能用作返回值。 In particular, notNull()
will actually return null and mark the "not null" match as a side-effect on a hidden stack, where it lingers until you interact with the mock next (when you actually invoke some_method
). 特别是, notNull()
实际上会返回null并将“ not null”匹配标记为在隐藏堆栈上的副作用,直到在您与下一个模拟交互之前(当您实际调用some_method
),它都会一直存在。
Though you didn't list your stack trace for your InvalidUseOfMatchersException
, I'll bet that the error actually happens when you invoke populateWithParameter
via some_method
, not when you stub populateWithParameter
. 尽管您没有列出InvalidUseOfMatchersException
堆栈跟踪,但我敢打赌,该错误实际上是在您通过some_method
调用 populateWithParameter
时发生的,而不是在对populateWithParameter
进行存根处理时发生populateWithParameter
。 The "1 recorded" matcher is notNull()
, where "4 matchers expected" refer to the number of arguments in the method call. “记录的1个”匹配器不是notNull()
,其中“预期的4个匹配器”是指方法调用中的参数数量。 The error message is really tailored for a case where you forget to use a matcher for some argument, like populateWithParameter(any(), any(), anyString(), 42)
, which is a very common mistake. 错误消息的确是针对以下情况量身定制的:您忘记为某些参数使用匹配器,例如populateWithParameter(any(), any(), anyString(), 42)
,这是一个非常常见的错误。
Though I see in the comments that "it does not work!" 尽管我在评论中看到“它不起作用!” when you try to return an instance, I can guarantee that returning notNull()
absolutely will cause a problem, whereas returning an instance may simply reveal a different problem. 当您尝试返回实例时,我可以保证绝对返回notNull()
会引起问题,而返回实例可能只是揭示了一个不同的问题。 You may want to update your question with the full stack trace after switching to returning an instance, or ask a new question. 切换到返回实例后,您可能想用完整的堆栈跟踪更新问题,或者提出新问题。
For more information about Mockito matchers behind the scenes, see my question/answer here . 有关幕后Mockito匹配器的更多信息,请在此处查看我的问题/答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.