【问题标题】:Dropwizard, Jersey, unit testing - GET throws ConstraintViolationException: The request entity was empty atDropwizard,Jersey,单元测试 - GET 抛出 ConstraintViolationException:请求实体在
【发布时间】:2014-08-05 15:13:49
【问题描述】:

我有 Dropwizard 应用程序,我正在为它编写单元测试。我正在关注 Dropwizard 的文档:http://dropwizard.readthedocs.org/en/latest/manual/testing.html

我缺少的是如何向调用 GET 方法的测试添加参数? 在文档中:

assertThat(resources.client().resource("/person/blah").get(Person.class)) .isEqualTo(person);

如果我的 get 方法有参数怎么办?

在 Jersey 的 WebResource 中有:

    @Override
    public <T> T get(Class<T> c) throws UniformInterfaceException, ClientHandlerException {
        return handle(c, build("GET"));
    }

    @Override
    public <T> T get(GenericType<T> gt) throws UniformInterfaceException, ClientHandlerException {
        return handle(gt, build("GET"));            
    }

【问题讨论】:

    标签: junit jersey dropwizard


    【解决方案1】:

    查询参数是资源 URI 的一部分。您可以将它们嵌入到字符串中:

    assertThat(resources.client().resource("/person/blah?a=b").get(Person.class)).isEqualTo(person);
    

    或者您可以动态构建 URI:

    URI resourceUri = UriBuilder.fromPath("/person/blah").queryParam("a", "b").build();
    assertThat(resources.client().resource(resourceUri).get(Person.class)).isEqualTo(person);
    

    【讨论】:

    • 是的,我这样做了,但这仅在被测试的 GET 方法没有参数时才有效。例如:@GET @Path("/packages") public List getPackagesList(@SessionAuth User user) { try { return user.getWorker().getPackagesInfo(user.getSessionKey()); } catch (Exception e) { throw new StatAceRestException(e);我有:测试 c.s.j.s.c.ContainerResponse:419 [main] - RuntimeException 无法映射到响应,重新抛出到 HTTP 容器 javax.validation.ConstraintViolationException: 请求实体为空
    • 方法参数如那些由 Dropwizard/Jersey 注入。大概您正在 Dropwizard 应用程序类中注册相应的提供程序; you also need to register it in your test class.
    • 非常感谢您的回复。不幸的是,这没有奏效。实际上,这里描述的问题是依赖项:严重:缺少依赖项是:无法将 RuntimeException 映射到响应,重新抛出到 HTTP 容器 javax.validation.ConstraintViolationException:io.dropwizard.jersey 的请求实体为空.jackson.JacksonMessageBodyProvider.validate(JacksonMessageBodyProvider.java:70)
    猜你喜欢
    • 2016-08-14
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2019-06-25
    • 2016-12-25
    • 1970-01-01
    相关资源
    最近更新 更多