Jersey JUnit测试框架 - 错误状态500

时间:2017-04-10 10:21:42

标签: java rest junit jersey

我想用球衣测试framework来测试我的球衣1应用。

我想测试一下:

@Path("fieldgen")
public class VRApi {

@Path("/testCon")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String testCon() {

    return "testcon";
}

@Path("/getTestObj")
@GET
@Produces(MediaType.APPLICATION_JSON)
public TestObj getField() {

    TestObj tobj= new TestObj ("str1", "str2", "str3");
    return tobj;
}

这是我的测试类:

public class TestMB extends JerseyTest{

private WebResource webResource = resource();

public TestMB() throws Exception {
    super("...api...");
}

@Test
public void testCon() {

    String responseMsg = webResource.path("/fieldgen/testCon").get(String.class);
    assertEquals("testcon", responseMsg);       
}


@Test
public void getTestObj()  {

    WebResource.Builder mbRes = webResource.path("/fieldgen/getTestObj").accept(MediaType.APPLICATION_JSON);
    TestObj vr = mbRes.get(TestObj.class);

    assertEquals(vr, new TestObj("str1", "str2", "str3"));
}   

第一次测试没问题。 第二次测试失败:

  A message body writer for Java class modelVR.TestObj, and Java type class 
  modelVR.TestObj, and MIME media type application/json was not found

我也试过使用ClientResponse。 POM dep。:

     <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-inmemory</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.3</version>
    </dependency>

问题是什么?谢谢。

2 个答案:

答案 0 :(得分:0)

你可能错过了&#34; jackson-databind&#34;其中包括自动POJO到JSON和反向映射器。 (杰克逊1.x的名字可能略有不同)

答案 1 :(得分:0)

检查您是否在客户端中注册了Pojo Mapping功能,如jersey docs here中所述。相关摘要如下所示。

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
 Client client = Client.create(clientConfig)