我有以下方法,我正在尝试使用Mockito编写单元测试.我对Mockito很新,并试图赶上.
测试方法
public synchronized String executeReadRequest(String url) throws Exception{
String result = null;
RestClient client = null;
Resource res = null;
logger.debug("Start executing GET request on "+url);
try{
client = getClient();
res = client.resource(url);
result = res.contentType(this.requestType).accept(this.responseType).get(String.class);
}
catch(Exception ioe){
throw new Exception(ioe.getMessage());
}
finally{
res = null;
client = null;
}
logger.info("GET request execution is over with result : "+result);
return result;
}
与Mockito进行单元测试
@Test
public void testRestHandler() throws Exception {
RestHandler handler = spy(new RestHandler());
RestClient mockClient = Mockito.mock(RestClient.class,Mockito.RETURNS_DEEP_STUBS);
Resource mockResource = Mockito.mock(Resource.class,Mockito.RETURNS_DEEP_STUBS);
doReturn(mockClient).when(handler).getClient();
Mockito.when(mockClient.resource(Mockito.anyString())).thenReturn(mockResource);
//ClassCastException at the below line
Mockito.when(mockResource.contentType(Mockito.anyString()).accept(Mockito.anyString()).get(Mockito.eq(String.class))).thenReturn("dummy read result");
handler.setRequestType(MediaType.APPLICATION_FORM_URLENCODED);
handler.setResponseType(MediaType.APPLICATION_JSON);
handler.executeReadRequest("abc");
}
但是我在线上得到了ClassCastException
Mockito.when(mockResource.contentType(Mockito.anyString()).accept(Mockito.anyString()).get(Mockito.eq(String.class))).thenReturn("dummy read result");
例外
java.lang.ClassCastException: org.mockito.internal.creation.jmock.ClassImposterizer$ClassWithSuperclassToWorkAroundCglibBug$$EnhancerByMockitoWithCGLIB$$4b441c4d cannot be cast to java.lang.String
感谢您解决此问题的任何帮助.
非常感谢.
最佳答案 在存根期间这种链接方式不正确:
Mockito.when(
mockResource.contentType(Mockito.anyString())
.accept(Mockito.anyString())
.get(Mockito.eq(String.class)))
.thenReturn("dummy read result");
即使你已经设置了模拟返回深层存根,Matchers work via side-effects,所以这条线不能达到你的想象.所有三个匹配器(anyString,anyString,eq)在调用when期间进行评估,并且您拥有代码的方式可能会在最轻微的挑衅时抛出InvalidUseOfMatchersException – 包括稍后添加不相关的代码或验证.
这意味着你的问题不是使用eq(String.class):Mockito正试图在不属于它的地方使用类匹配器.
相反,你需要专门存根:
Mockito.when(mockResource.contentType(Mockito.anyString()))
.thenReturn(mockResource);
Mockito.when(mockResource.accept(Mockito.anyString()))
.thenReturn(mockResource);
Mockito.when(mockResource.get(Mockito.eq(String.class))) // or any(Class.class)
.thenReturn("dummy read response");
请注意,这里的一些困难是Apache Wink使用了Builder模式,这在Mockito中可能很费力. (我已经在这里返回了mockResource,但你可以想象返回特定的其他Resource对象,代价是以后要求它们完全按顺序.)更好的方法可能是use a default Answer that returns this
whenever possible.