无法使用 Mockito 返回类对象
我正在尝试编写一个单元测试,为此我正在为 Mockito 模拟编写一个 when 语句,但我似乎无法让 eclipse 认识到我的返回值是有效的.
I'm trying to write a unit test, and to do that I'm writing a when statement for a Mockito mock, but I can't seem to get eclipse to recognize that my return value is valid.
这就是我正在做的事情:
Here's what I'm doing:
Class<?> userClass = User.class;
when(methodParameter.getParameterType()).thenReturn(userClass);
.getParameterType()
的返回类型是Class<?>
,所以不明白eclipse为什么说,方法thenReturn(Class<capture#1-of ?>) 类型为 OngoingStubbing<Class<capture#1-of ?>>不适用于参数 (Class<capture#2-of ?>)
.它提供了转换我的 userClass,但这只会使一些乱码 eclipse 说它需要再次转换(并且不能转换).
The return type of .getParameterType()
is Class<?>
, so I don't understand why eclipse says, The method thenReturn(Class<capture#1-of ?>) in the type OngoingStubbing<Class<capture#1-of ?>> is not applicable for the arguments (Class<capture#2-of ?>)
. It offers to cast my userClass, but that just makes some garbled stuff eclipse says it needs to cast again (and can't cast).
这只是 Eclipse 的问题,还是我做错了什么?
Is this just an issue with Eclipse, or am I doing something wrong?
推荐答案
另外,一个更简洁的解决方法是使用 do 语法而不是 when.
Also, a slightly more terse way to get around this is to use the do syntax instead of when.
doReturn(User.class).when(methodParameter).getParameterType();
相关文章