我想使用 Jersey 测试 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。 POM部门:
<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>
有什么问题吗?谢谢。
最佳答案
您可能缺少“jackson-databind”,其中包括自动 POJO 到 JSON 和反向映射器。 (也许 Jackson 1.x 中的名称略有不同)
关于java - Jersey JUnit 测试框架 - 错误状态 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320655/