1

在尝试获取在 Jersey Web 服务发布调用中作为参数传递的 HttpServletRequest 后,我​​的单元测试失败了:

@RequestScoped
@Path("/por/req")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class MyOwnWs {

@POST
@Path("/confirm")
public Response confirmarRequerimento(
        @ApiParam(value = "Description", required = false)
        final InputJson input,
        @Context HttpServletRequest request) throws MyException {


   Response response = null;
   MyResult result = null;

   String ipAdress = IpUtil.getIp(request);

   // do stuff

   return response;
}
}

在浏览器中测试很好,请求对象包含我的测试机IP地址。但是在带有 grizzly 的单元测试中,请求对象始终为空,我仍然没有找到模拟它的方法。我的测试代码的相关部分如下:

MyResult response = target(URL).
            request().
            post(Entity.json(input), MyResult.class);

这在服务调用中添加 @Context HTTPServletRequest 之前工作正常。关于我所缺少的任何建议?

4

0 回答 0