我试图找到一些手册如何使用泽西框架测试POST方法,只有GET方法的例子.
这里的例子:
@POST @Path("add") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response addUser(JAXBElement<User> user) { int code = userService.addUser(user.getValue()); if (code == 500) { return Response.status(500).build(); } return Response.status(code).entity(user).build(); }
你能否发表一些POST方法测试示例?
先谢谢你.
解决方法
经过研究我做到了!
这是我的解决方案,它的工作原理很好.
而且它是相当集成的测试,但我们可以以类似的方式编写单元测试.
public class RestTest extends JerseyTest{ @Override protected Application configure() { return new Your_Resource_Config(); //Your resource config with registered classes } //@Before and/or @After for db preparing etc. - if you want integration tests @Test public void addUserTest() { User user = new User(); user.setEmail("user2@mail.com"); user.setName("Jane Doe"); user.getUserRoles().getRoles().add("supertester"); Entity<User> userEntity = Entity.entity(user,MediaType.APPLICATION_XML_TYPE); target("users/add").request().post(userEntity); //Here we send POST request Response response = target("users/find").queryParam("email","user2@mail.com").request().get(); //Here we send GET request for retrieving results Assert.assertEquals("user2@mail.com",response.readEntity(User.class).getEmail()); }