我在创建用于单元测试的模拟Response对象时遇到问题.我使用org.glassfish.jersey.core.jersey-client版本2.3.1来实现我的RESTful客户端和mockito版本1.9.5来帮助我使用模拟对象.这是我的测试代码:
@Test
public void testGetAll() throws IOException {
// Given
String expectedResource = "expectedResource"
final Response expectedRes = Response.ok(expectedResource, MediaType.APPLICATION_JSON).build();
String receivedResource;
BDDMockito.given(this.client.getSimpleClient().getAllWithResponse()).willReturn(expectedRes);
// When
receivedResource = this.client.getAll();
// Then
Assert.assertNotNull("Request constructed correctly and response received.", receivedResource);
Assert.assertEquals("Resource is equal to expected.", expectedResource, receivedResource);
}
Run Code Online (Sandbox Code Playgroud)
this.client.getAll();执行时会出现问题.这是该方法的代码:
public String getAll() throws GenericAragornException, ProcessingException{
Response response = this.simpleClient.getAllWithResponse();
if (response.getStatus() != 200) {
processErrorResponse(response);
}
String entity = response.readEntity(String.class);
// No errors so return entity converted to …Run Code Online (Sandbox Code Playgroud) 当我尝试模拟javax.ws.rs.core时,Response我收到一条错误消息:
无法创建JAX-RS运行时委托
为什么会这样?
Response response = Mockito.mock(Response.class);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试模拟HttpServletResponse时,没有问题!
HttpServletResponse response1 = Mockito.mock(HttpServletResponse.class);
Run Code Online (Sandbox Code Playgroud) 我无法理解如何解析javax.ws.rs.core.Response.有些人指出使用InputStream,但我不理解它是如何工作的,因为response.getEntity()的返回类型是Object类型.例如:
Response response = client.target(enpoint).request(MediaType.APPLICATION_XML).get();
InputStream is = response.getEntity();
Run Code Online (Sandbox Code Playgroud)
NetBeans抱怨并说我需要将Object类型转换为InputStream.响应将由XML组成,我只是希望能够使用DOM解析它.我无法从javax.ws.rs.core.Response获取任何有用的东西.
有任何想法吗?