我正在尝试模拟Spring的MessageSource.getMessage
方法,但Mockito
它抱怨一条无用的消息,我正在使用:
when(mockMessageSource.getMessage(anyString(), any(Object[].class), any(Locale.class)))
.thenReturn(anyString());
错误消息是:
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods
that cannot be mocked Following methods *cannot* be stubbed/verified: final/private/equals()
/hashCode().
知道我在做什么错吗?
最佳答案
我相信问题是anyString()
是它在您的thenReturn(...)
调用中用作参数时抱怨的匹配器。如果您不关心返回的内容,则只需返回一个空字符串 thenReturn("")
。