Vadym Roganin Vadym Roganin - 2 years ago 118
reST (reStructuredText) Question

Jersey Test Framework 2.5 - test POST method

I'm trying to find some manual how to test

methods using
framework, only got examples for

Here's example:

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();


Could you please post some POST method test example?
Thank you in advance.

Answer Source

After research I did it!

Here's my solution, it works just fine. And it's rather integration test, but we can write unit tests in similar manner.

public class RestTest extends JerseyTest{

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

public void addUserTest() {
    User user = new User();
    user.setEmail("[email protected]");
    user.setName("Jane Doe");
    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", "[email protected]").request().get(); //Here we send GET request for retrieving results
    Assert.assertEquals("[email protected]", response.readEntity(User.class).getEmail());

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download