我正在尝试编写一个单元测试,为此我正在为 Mockito 模拟编写一个 when 语句,但我似乎无法让 eclipse 认识到我的返回值是有效的。
这就是我正在做的事情:
Class<?> userClass = User.class;
when(methodParameter.getParameterType()).thenReturn(userClass);
.getParameterType()
的返回类型是 Class<?>
,所以我不明白为什么 eclipse 说,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 ?>)
.它提供了转换我的 userClass,但这只会使一些乱码 eclipse 说它需要再次转换(并且不能转换)。
这只是 Eclipse 的问题,还是我做错了什么?
最佳答案
此外,解决此问题的一种更简洁的方法是使用 do 语法而不是 when。
doReturn(User.class).when(methodParameter).getParameterType();
关于java - 无法使用 Mockito 返回类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16890133/