簡體   English   中英

測試Jersey REST資源(無效的返回響應)

[英]Testing Jersey REST resource (invalid return response)

我正在嘗試測試特定的球衣資源,但是球衣客戶InboundJaxrsResponse在返回響應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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM