use*_*834 5 java junit spring mockito
我试图模拟 Spring 的MessageSource.getMessage方法,但Mockito它抱怨一条无用的消息,我正在使用:
when(mockMessageSource.getMessage(anyString(), any(Object[].class), any(Locale.class)))
.thenReturn(anyString());
Run Code Online (Sandbox Code Playgroud)
错误信息是:
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().
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
虽然接受的答案修复了问题中的代码,但我想指出,没有必要使用模拟库来创建MessageSource始终返回空字符串的库。
以下代码执行相同的操作:
MessageSource messageSource = new AbstractMessageSource() {
protected MessageFormat resolveCode(String code, Locale locale) {
return new MessageFormat("");
}
};
Run Code Online (Sandbox Code Playgroud)
我认为,问题在于,当anyString()它在调用中用作参数时,它会抱怨匹配器thenReturn(...)。如果您不关心返回什么,只需返回一个空 String a la thenReturn("")。
| 归档时间: |
|
| 查看次数: |
13637 次 |
| 最近记录: |