Java – Jersey test framework 2.5 – test post method
•
Java
I tried to find some manuals on how to use the Jersey framework to test post methods, only get methods
Here's an example:
@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(); }
Can you publish some post method test examples? Thank you first
Solution
After research, I did it!
This is my solution. It works well And it's a fairly integrated test, but we can write unit tests in a similar way
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()); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码