无法使用Mockito返回Class对象 [英] Can't return Class Object with Mockito

查看:172
本文介绍了无法使用Mockito返回Class对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个单元测试,为此我正在为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语法而不是何时。

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();

这篇关于无法使用Mockito返回Class对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆