title: Mocking response for HTTPGet in Java
body: <p>I have to mock a request to an API that returns a response with JSON entity
  For this I mock the get request as well as the JSON object</p>
  
  <pre><code>public class RestTest {
      static JSONObject job;
      static JSONArray portsArray;
      static JSONArray routesArray;
      static JSONObject routeObject;
      private static final HttpClient  client = mock(DefaultHttpClient.class);
      private static final HttpGet  get = mock(HttpGet.class);
      private static final HttpResponse response = mock(CloseableHttpResponse.class);
      private static  HttpEntity entity = mock(HttpEntity.class);
  
      @BeforeClass
      public static void setup() throws ClientProtocolException, IOException, JSONException {
          HttpGet getRoute = new HttpGet("api/to/access");
          getRoute.setHeader("Content-type", "application/json");
          JSONObject routesJson = new JSONObject();
          routesJson.put("","");
          when(response.getEntity()).thenReturn(entity);
          when(response.getEntity().getContent().toString()).thenReturn(routesJson.toString());
          when(client.execute(getRoutes)).thenReturn(response);
      }
  }
  </code></pre>
  
  <p>This returns a null pointer at         <code>when(response.getEntity().getContent().toString()).thenReturn(routesJson.toString());</code></p>
  
  <p>How do I properly mock the JSON object so when the real request is executed it returns the mocked JSON?</p>
  
  <p>I am unable to set <code>entity.setContent()</code> as seen in examples since the method does not exist.</p>