java Jersey JUnit测试框架错误状态500
我想用jersey test framework测试我的jersey 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。 警察局副局长:
<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>
有什么问题吗?谢谢
# 1 楼答案
检查是否已按照jersey docs here中的说明在客户端注册Pojo映射功能。下面给出了相关片段