繁体   English   中英

Mockito的Mocking Jersey客户ClientResponse

[英]Mocking Jersey Client ClientResponse with Mockito

我在模拟com.sun.jersey.api.client.ClientResponse时遇到一些问题,但是仅当我设置.type(MediaType.MULTIPART_FORM_DATA_TYPE时。

我被球衣客户端1.18困扰。

这是测试中的代码:

 ClientResponse clientResponse = client.resource(url)
            .accept("application/json")
            .entity(multiPart)
            .type(MediaType.MULTIPART_FORM_DATA_TYPE)
            .post(ClientResponse.class);

这是测试的模拟:

 when(clientResponse.getEntity(String.class)).thenReturn(body);
 when(builder.post(eq(ClientResponse.class))).thenReturn(clientResponse);
 when(builder.type(MediaType.MULTIPART_FORM_DATA_TYPE)).thenReturn(builder);
 when(webResource.accept(anyString())).thenReturn(builder);
 when(client.resource(anyString())).thenReturn(webResource);;

我收到的错误是受测代码中的NullPointerException行:

 .type(MediaType.MULTIPART_FORM_DATA_TYPE)

有人知道如何模拟Client.resource()。type()吗?

如果我了解您在做什么,那么您就嘲笑了一个构建器。

您没有模拟方法涵盖在webResource.accept()返回的builder上调用builder.entity() ,因此它返回null并且链中的下一个调用失败( builder.type() )。

加:

 when(builder.entity(anyString())).thenReturn(builder);

(提供的multiPartString

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM