On Aug 22, 2012, at 3:53 PM, Adam Parrish wrote:
> The above code should throw a WebApplicationException when the user already exists or if a bad email is passed in. Here is an example unit test I would hope should work:
>
> @Test(expected = WebApplicationException.class)
> public void testAddNonUniqueEmail() {
> insertTestData();
> User user = new User("Different Name", "
some...@gmail.com", new Password("pw555666"));
> client().resource(new MemmeeURLBuilder().setBaseURL(UserResource.BASE_URL).setMethodURL("user").build()).post(user);
> }
...
> However I get the following exception instead of the expected one:
Your expectation is incorrect. JAX-RS 1.x does not define a client API, so there's no guarantee that you'll receive a WebApplicationException. As you've discovered the Jersey client raises an exception of its own type.
> Is there a documented correct way to do this? Also is there a more appropriate way to bubble up the exceptions to the client using this API?
If you are not interested in the HTTP response code, then just replace WebApplicationException with UniformInterfaceException as your expected exception.
However, if you are interested (e.g., testing entity validation), then you need to capture the client response. For example,
ClientResponse response = client().resource(new MemmeeURLBuilder().setBaseURL(UserResource.BASE_URL).setMethodURL("user").build()).post(ClientResponse.class, user);
assertEquals(ClientResponse.Status.BAD_REQUEST, response.getClientResponseStatus());
--
Christopher Elkins