模拟 ResourceInfo 的 getResourceClass

mocking getResourceClass of ResourceInfo

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

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