测试 Jersey REST 资源(无效 return 响应)
Testing Jersey REST resource (invalid return response)
我正在尝试测试特定的球衣资源,但球衣客户端返回响应 InboundJaxrsResponse
,而我期待 OutboundJaxrsResponse
。我不明白这种行为。
我确实使用调试器进行了调查,资源按预期返回了 OutboundJaxrsResponse
,这意味着球衣客户端正在某处生成 wrapping/conversion,但我不明白为什么。
如果我做的不对,你能告诉我一个比较响应的好方法吗?
我正在使用 dropwizard。
@Test
public void itShouldRetrieveListOfComputations() {
List<Computation> computations = new ArrayList<Computation>();
computations.add(new Computation("name1", "description1", "expression1"));
computations.add(new Computation("name2", "description2", "expression2"));
when(computationDAO.findAll()).thenReturn(computations);
Response expected = Response.ok(computations).build();
assertThat(resource.client().target("/computations").request().get()).isEqualTo(expected);
verify(computationDAO).findAll();
}
被测资源
@GET
@UnitOfWork
@Timed
public Response list() {
List<Computation> computations = computationDAO.findAll();
Response response = Response.ok(computations).build();
return response;
}
后果
org.junit.ComparisonFailure:
Expected :OutboundJaxrsResponse{status=200, reason=OK, hasEntity=true, closed=false, buffered=false}
Actual :InboundJaxrsResponse{context=ClientResponse{method=GET, uri=/computations, status=200, reason=OK}}
Response.ok(computations).build();
创建 Jersey 用于发送给客户端的传出响应。您不能真正使用它来与来自客户呼叫的传入响应进行比较。它们是抽象 class.
的两种不同实现
这是我通常验证回复的方式:
Response response = resource.client().target("/computations").request().get(Response.class);
assertThat( response.getStatusInfo.getFamily() ).isEqualTo( Response.Status.Family.Success );
assertThat( response.getMediaType() ).isEqualTo( MediaType.APPLICATION_JSON_TYPE );
assertThat( response.readEntity(new GenericType<List<Computation>>() {}) ).isEqualTo( computations );
我正在尝试测试特定的球衣资源,但球衣客户端返回响应 InboundJaxrsResponse
,而我期待 OutboundJaxrsResponse
。我不明白这种行为。
我确实使用调试器进行了调查,资源按预期返回了 OutboundJaxrsResponse
,这意味着球衣客户端正在某处生成 wrapping/conversion,但我不明白为什么。
如果我做的不对,你能告诉我一个比较响应的好方法吗?
我正在使用 dropwizard。
@Test
public void itShouldRetrieveListOfComputations() {
List<Computation> computations = new ArrayList<Computation>();
computations.add(new Computation("name1", "description1", "expression1"));
computations.add(new Computation("name2", "description2", "expression2"));
when(computationDAO.findAll()).thenReturn(computations);
Response expected = Response.ok(computations).build();
assertThat(resource.client().target("/computations").request().get()).isEqualTo(expected);
verify(computationDAO).findAll();
}
被测资源
@GET
@UnitOfWork
@Timed
public Response list() {
List<Computation> computations = computationDAO.findAll();
Response response = Response.ok(computations).build();
return response;
}
后果
org.junit.ComparisonFailure:
Expected :OutboundJaxrsResponse{status=200, reason=OK, hasEntity=true, closed=false, buffered=false}
Actual :InboundJaxrsResponse{context=ClientResponse{method=GET, uri=/computations, status=200, reason=OK}}
Response.ok(computations).build();
创建 Jersey 用于发送给客户端的传出响应。您不能真正使用它来与来自客户呼叫的传入响应进行比较。它们是抽象 class.
这是我通常验证回复的方式:
Response response = resource.client().target("/computations").request().get(Response.class);
assertThat( response.getStatusInfo.getFamily() ).isEqualTo( Response.Status.Family.Success );
assertThat( response.getMediaType() ).isEqualTo( MediaType.APPLICATION_JSON_TYPE );
assertThat( response.readEntity(new GenericType<List<Computation>>() {}) ).isEqualTo( computations );