我正在尝试测试特定的 Jersey 资源,但 Jersey 客户端正在返回响应 InboundJaxrsResponse
,而我期待 OutboundJaxrsResponse
。我不明白这种行为。
我确实使用调试器进行了调查,并且资源按预期返回了 OutboundJaxrsResponse
,这意味着 Jersey 客户端正在某处进行包装/转换,但我不明白为什么。
如果我做得不对,你能告诉我一个比较响应的好方法吗?
我正在使用 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 用于发送给客户端的传出响应。您不能真正使用它来与来自客户端调用的传入响应进行比较。它们是抽象类的两种不同实现。
这就是我通常验证响应的方式:
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 );
关于java - 测试 Jersey REST 资源(无效返回响应),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954471/