Unable to Mock Glassfish Jersey Client response object

I had this error because when you use the ResponseBuilder, it returns an OutboundJaxrsResponse message that can not be processed with readEntity().

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 use readEntity() and had the same error as you.

defaultController.get();

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 the readEntity() method.

target("/default").request.get();

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 my DefaultController method.

Without mocking the readEntity() method, I suggest you to find a way to get your response processed by Jersey and turned into a ScopedJaxrsResponse.

Hope that helps.


You can also mock response with Mockito:

final Response response = Mockito.mock(Response.class);
Mockito.when(response.getStatus()).thenReturn(responseStatus);
Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(responseEntity);
Mockito.when(response.readEntity(Mockito.any(GenericType.class))).thenReturn(responseEntity);

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.


Rather than using readEntity(OutputClass.class) you can do something like:

OutputClass entity = (OutputClass)outboundJaxrsResponse.entity

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 do response.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. The readEntity method is expecting to be called at the point of deserialization.

As other posters have observed, readEntity on an outbound Response 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:

  /**
   * Turn a locally made {@link Response} into one which can be used as though inbound.
   * This enables readEntity to be used on what should be an outbound response
   * outbound responses don't support readEntity, but we can fudge it using
   * mockito.
   * @param asOutbound response built as though being sent to the received
   * @return a spy on the response which adds reading as though inbound
   */
  public static Response simulateInbound(Response asOutbound) {

    Response toReturn = spy(asOutbound);
    doAnswer(answer((Class<?> type) -> readEntity(toReturn, type)))
        .when(toReturn)
        .readEntity(ArgumentMatchers.<Class<?>>any());
    return toReturn;
  }

  // use the getEntity from the real object as
  // the readEntity of the mock
  @SuppressWarnings("unchecked")
  private static <T> T readEntity(Response realResponse, Class<T> t) {
    return (T)realResponse.getEntity();
  }