I am having problems with creating a mock Response object to use with my unit tests. I am using org.glassfish.jersey.core.jersey-client
version 2.3.1 to implement my RESTful client and mockito
version 1.9.5 to help me with mock objects. Here is my test's code:
@Test
public void testGetAll() throws IOException {
// Given
String expectedResource = "expectedResource"
final Response expectedRes = Response.ok(expectedResource, MediaType.APPLICATION_JSON).build();
String receivedResource;
BDDMockito.given(this.client.getSimpleClient().getAllWithResponse()).willReturn(expectedRes);
// When
receivedResource = this.client.getAll();
// Then
Assert.assertNotNull("Request constructed correctly and response received.", receivedResource);
Assert.assertEquals("Resource is equal to expected.", expectedResource, receivedResource);
}
The problem occurs when this.client.getAll();
is executed. Here is that method's code:
public String getAll() throws GenericAragornException, ProcessingException{
Response response = this.simpleClient.getAllWithResponse();
if (response.getStatus() != 200) {
processErrorResponse(response);
}
String entity = response.readEntity(String.class);
// No errors so return entity converted to resourceType.
return entity;
}
Note that I am mocking the this.simpleClient.getAllWithResponse() method with the manually created Response. When it reaches the response.readEntity(resourceListType);
instruction, Jersey throws the following exception: java.lang.IllegalStateException - Method not supported on an outbound message.
. After lots of research and debugging, it turns that, for some reason, when I create a Response using the response builder such as Response.ok(expectedResource, MediaType.APPLICATION_JSON).build();
it creates it as an OutboundResponse instead of as an InboundResponse. The latter are the only ones permitted to use the Response.readEntity()
method. If it is an OutboundResponse, the exception is thrown.
However, I could not find any way of converting the manually created response to an InboundResponse. So my tests are doomed :(. Do you guys/gals have any idea of what I can do here? I don't want to mock the Response object with Mockito because I think it could be a code smell since it violates the Law of Demeter. Sincerely I am out of ideas here. Things like this should be simple and straightforward.
I solved mocking the responses:
These can help if your steer is to manually create a ScopedJaxrsResponse instead:
I hit this problem myself, trying to mock a client to a remote service by using
Response.ok(entity).build()
and then allow my client code to doresponse.readEntity(...)
on the response from my faked up server.I discuss the subject in https://codingcraftsman.wordpress.com/2018/11/26/testing-and-mocking-jersey-responses/
The issue is that the
Response.build()
method is intended to produce an outbound response, which is meant to be serialized before being received and deserialized by a real client. ThereadEntity
method is expecting to be called at the point of deserialization.As other posters have observed,
readEntity
on an outboundResponse
will give you the exact entity object you put in. You can even cast it to whatever type you want. Of course this doesn't work on a real inbound response, since the inbound response just has the text/binary of the inbound stream from the server.I wrote the following code to allow me to use mockito to force an outbound
Response
to pretend to be an inbound one:I had this error because when you use the
ResponseBuilder
, it returns anOutboundJaxrsResponse
message that can not be processed withreadEntity()
.I noticed that I had this error only when I was calling the Jax-RS component directly. For exemple, if I have
DefaultController
annotated with@Path("/default")
and if I tried to directly call its method, I could not usereadEntity()
and had the same error as you.Now, when I was using the grizzly2 test provider and using a client to target the Rest Url (in the previous case, it is
/default
), the message I received in response was a ScopedJaxrsResponse. And then I could use thereadEntity()
method.In your case, you mocked the simpleClient in order to reply with a response built with
ResponseBuilder
that is not processed by jersey. It's comparable to calling directly myDefaultController
method.Without mocking the
readEntity()
method, I suggest you to find a way to get your response processed by Jersey and turned into aScopedJaxrsResponse
.Hope that helps.
Test code:
Mocking:
For me, none of these answers worked because I was trying to write a server-side unit test that tested the body of the generated
Response
instance, itself. I got this exception by calling a line likeString entity = readEntity(String.class)
. Instead of mockingResponse
object, I wanted to test it.The fix for me was to substitute the above problematic line to one like:
You can also mock response with Mockito:
responseStatus is status code associated with the response and responseEnitty is of course entity you want to return. You can use that mock as a return statment in Mockito (e.g. ... thenReturn(response)).
In my projects I create builders for different types for mocks (in that case Response), so I can easly on demand build required mocks, e.g. Response with status code 200 and some custom entity attached.