title: Unable to Mock Glassfish Jersey Client response object
body: <p>I am having problems with creating a mock Response object to use with my unit tests. I am using <code>org.glassfish.jersey.core.jersey-client</code> version 2.3.1 to implement my RESTful client and <code>mockito</code> version 1.9.5 to help me with mock objects. Here is my test's code:</p>
  
  <pre><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);
  }
  </code></pre>
  
  <p>The problem occurs when <code>this.client.getAll();</code> is executed. Here is that method's code:</p>
  
  <pre><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;
  }
  </code></pre>
  
  <p>Note that I am mocking the this.simpleClient.getAllWithResponse() method with the manually created Response. When it reaches the <code>response.readEntity(resourceListType);</code> instruction, Jersey throws the following exception: <code>java.lang.IllegalStateException - Method not supported on an outbound message.</code>.  After lots of research and debugging, it turns that, for some reason, when I create a Response using the response builder such as <code>Response.ok(expectedResource, MediaType.APPLICATION_JSON).build();</code> it creates it as an OutboundResponse instead of as an <strong>InboundResponse</strong>. The latter are the only ones permitted to use the <code>Response.readEntity()</code> method. If it is an <strong>OutboundResponse</strong>, the exception is thrown.</p>
  
  <p>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.</p>