添加Jersey测试工具的Maven依赖
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jdk-http</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
简单的Jersey Restful测试示例
DemoRestful.java
@Path("demo")
public class DemoRestful {
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String test(String str) {
return "This is a jersey restful test method.";
}
}
DemoRestfulTest.java
public class DemoRestfulTest extends JerseyTest {
@Override
protected void configureClient(ClientConfig config) {
config.register(FastJsonFeature.class);
}
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ResourceConfig config = new ResourceConfig();
config.register(FastJsonFeature.class);
config.packages("com.faw_qm.cloud.platform.*.restful");
return config;
}
@Test
public void test() throws Exception {
JSONObject json = new JSONObject();
json.put("name", "test");
Response response = target("demo").path("test").request().
accept("application/json;charset=UTF-8").post(Entity.json(json));
Assert.assertEquals(response.getStatus(), 200);
}
}