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
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);
}
}
This returns a null pointer at when(response.getEntity().getContent().toString()).thenReturn(routesJson.toString());
How do I properly mock the JSON object so when the real request is executed it returns the mocked JSON?
I am unable to set entity.setContent()
as seen in examples since the method does not exist.