I had misunderstood the problem and the solution was obvious once I got it.
The exception thrown was not from the Resource but occured inside the HTTP
client when trying to parse the Response as the supplied Video.class.
The solution to this problem is much more straight forward as illustrated
below.
Response response = resources.client()
.target("/video/e5ed29b8-c2da-11e7-b440-4ccc6ae15c7e")
.request()
.get();
assertThat(response.readEntity(String.class)).isEqualTo("{"code":404,"message":"Video
e5ed29b8-c2da-11e7-b440-4ccc6ae15c7e does not exist."}");
On Tuesday, 7 November 2017 11:31:55 UTC+1, Pontus Alexander Persson wrote:
>
> I have recently started exploring Dropwizard and I don't understand how to
> properly test errors emitted from resource endpoints.
>
> In the endpoint below I throw a javax.ws.rs.BadRequestException.
>
> https://gist.github.com/pomle/734e68096aa7307ae58a5f1efa849fa3#file-resource-java-L27
>
> This generates a nice JSON formatted error when probed using `curl`.
>
> HTTP/1.1 400 Bad Request
>> Date: Tue, 07 Nov 2017 10:21:09 GMT
>> Content-Type: application/json
>> Content-Length: 83
>> {"code":400,"message":"Video ac4827da-c2e4-11e7-8b6a-4ccc6ae15a71 already
>> exists."}
>
>
>
> When I write a test for this I use the ResourceTestRule to create a HTTP
> client but when the request is executed a javax.ws.rs.ClientErrorException
> is thrown.
>
> https://gist.github.com/pomle/734e68096aa7307ae58a5f1efa849fa3#file-resourcetest-java-L46
>
> As far as I can tell the ClientErrorException does not contain the
> information from the original exception.
>
> I have tried writing the test by calling the resource method directly on
> the Resource class.
>
> https://gist.github.com/pomle/734e68096aa7307ae58a5f1efa849fa3#file-resourcetest-java-L63
>
> How can I structure my test using the ResourceTestRule and assert that
> the error is of the expected type and has the expected message?
>
> Thank you!
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.