2

We're using javax.ws.rs.core.Response.readEntity(Class type) to parse JSON responses into POJOs. I want to write tests to assure that the entity is correctly mapped into the POJO - so if a boolean "valid" is true in the json, it is also true in the POJO - and vice versa.

I cannot figure out how to do it. Any ideas or tips?

oyvind.s
  • 232
  • 1
  • 15

2 Answers2

3

I want to write tests to assure that the entity is correctly mapped into the POJO

If all you want to test is that the POJO is mapped correctly, and you are using Jackson as the JSON provider, you can simply use the ObjectMapper to deserialize the JSON. This is how Dropwizard recommends testing models

final ObjectMapper mapper = new ObjectMapper();
POJO pojo = mapper.readValue(jsonString, POJO.class);
// assertions
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

Testing marshalling is one of those things in jax-rs services that are not easy to test with plain junit tests. For instance, how can you ensure you did not forget an annotation @Path or @QueryParam, so that your junit test calling the method succeeds, but actually calling the service fails.

Arquillian provides a solution for those tests. Essentially, Arquillian enables executing tests in a running javaee container. The test setup includes building your war, and deploying your application. This way, you can test all elements of your stack, and make sure all goes together: marshalling, request routing, http filters,...

If you are using the jersey jax-rs stack, you can alternatively use JerseyTest.

Arquillian is more complete, since it enables testing with multiple containers (even selenium support) and does not tie you with a jax-rs implementation.

tonio
  • 10,355
  • 2
  • 46
  • 60
  • Note that I only want to test clients that we build to consume our APIs. And these clients are decoupled from the APIs - so all I want to do is to somehow provide the expected response from the API and assert that my client handles it as expected. So yes - I was hoping to do it with plain junit tests - if possible. – oyvind.s Aug 14 '17 at 09:51