无法在Jersey Test Framework中找到内容类型为application / json的邮件正文阅读器

时间:2017-04-18 12:12:09

标签: java junit jersey jersey-test-framework

我选择了Jersey测试框架来实现REST服务的单元测试用例。但是一旦我运行测试,我就会遇到以下问题。

注意:我甚至将resteasy-jackson-provider添加到pom文件中但无法帮助。

这是.pom文件依赖

<!-- jersey security dependency  -->
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet-core</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.media</groupId>
                <artifactId>jersey-media-multipart</artifactId>
                <version>${jersey.version}</version>
            </dependency> 

            <dependency>
              <groupId>org.glassfish.jersey.media</groupId>
              <artifactId>jersey-media-json-jackson</artifactId>
               <version>${jersey.version}</version>
            </dependency>
            <!-- jersey test framework dependency  -->

         <dependency>

          <groupId>org.glassfish.jersey.test-framework.providers</groupId>
          <artifactId>jersey-test-framework-provider-jetty</artifactId>
          <version>${jersey.version}</version>
          <scope>test</scope>
        </dependency>

        <dependency>
          <groupId>org.jboss.resteasy</groupId>
          <artifactId>resteasy-jackson-provider</artifactId>
          <version>2.3.4.Final</version>
       </dependency>

        <!--junit Dependency-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>

MockServices.Java

@Path("/hello")
public class MockServices {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/world")
    public DateVO getHello() {
        DateVO j=new DateVO ();
        j.setActive(true);
        return j;
    }
}

MockServicesTest.Java

    public class MockServicesTest extends JerseyTest {




        @Override
        protected Application configure() { 
            return new ResourceConfig(MockServices.class);
        }



        @Test
        public void test() {
            Response hello = target("/hello/world").request().get();
            System.out.println(hello.readEntity(String.class));//throw an above exception
        }

}

请让我知道如何克服这个问题。

2 个答案:

答案 0 :(得分:0)

像这样覆盖你的提供者方法

@Override
    protected Application configure() { 
       ResourceConfig config =new ResourceConfig(MockServices.class).register(JacksonFeature.class).register("Your ContextResolver<ObjectMapper> implementation class");
 return config;
    }

答案 1 :(得分:0)

我必须使用显式的Jersey客户端实现来调用REST端点。

@Test
 public void test() {
    final Client client = new JerseyClientBuilder().build();
    WebTarget target = client.target("http://localhost:9998");
    final Response response = 
    target.path("/hello/world").request().get();
    final String json = response.readEntity(String.class);

}

Reference