0

I have a resource:

@Path("/")
public class Resource {

   @GET
   public Response getResponse() {
       //..
       final GenericEntity<List<BusinessObject>> entity = new GenericEntity<List<BusinessObject>>(businessobjects) { };
       return Response.status(httpResultCode).entity(entity).build();
   }

}

I want to unit test this method without using a Jersey client, but I don't know how to get the body of the Response object. I can't see a method that works. Here's the test method:

@Test
public void testMethod() {
    Resource resourceUnderTest = new Resource();
    Response response = resourceUnderTest.getResponse();
    List<BusinessObject> result = ???;
}

I can get the result I want if I go though a Jersey Client, but I would rather just call the method directly without making any HTTP requests.

4

1 回答 1

2
List<BusinessObject> result = (List<BusinessObject>)response.getEntity();

这将返回您传递给响应构建器的实体方法的对象。Response 对象不序列化结果。查看前面的方法,getEntity 可能会返回 GenericEntity>,所以您需要这样的代码。

GenericEntity<List<BusinessObject>> result = (GenericEntity<List<BusinessObject>>)response.getEntity();
于 2012-06-01T21:58:10.670 回答