我正在尝试模拟getResourceClassjavax.ws.rs.container.ResourceInfo。所以我所做的是:

ResourceInfo resourceInfo = mock(ResourceInfo.class);


现在,当我在下面尝试时:

when(resourceInfo.getResourceClass()).thenReturn(Class.forName("com.p.q.ClassName"));


它引发以下编译错误:

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 ?>)


谁能帮我解决这个问题。谢谢。

最佳答案

Mockito用户不是很大,所以我无法真正解释为什么它不起作用。但是经过一番游戏之后,我发现这可行

ResourceInfo resourceInfo = Mockito.mock(ResourceInfo.class);
Mockito.doReturn(YouResourceClass.class).when(resourceInfo).getResourceClass();


另外一个选项

Mockito.<Class<?>>when(resourceInfo.getResourceClass()).thenReturn(YourResource.class);

关于java - 模拟ResourceInfo的getResourceClass,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28501900/

10-12 12:50