RemoteAgent extends AbstractAgent { public boolean ping() { HttpResponse httpResponse = client.execute(HttpRequest.get(pingUrl)); if (httpResponse.isSuccess()) { String resp = httpResponse.asString(); try { String text = gson.fromJson(resp, String.class); return text.equals(PING_RESPONSE); } catch (JsonSyntaxException e) { logger.warn("Invalid json for ping response: " + resp); } } return false; } RemoteAgent(String urlBase, HttpClient client); @Override AgentResponse process(AgentRequest request); boolean ping(); String getUrlBase();  }
@Test public void testPing() { HttpResponse httpResponse1 = mock(HttpResponse.class); when(httpResponse1.asString()).thenReturn("\"pong\""); when(httpResponse1.isSuccess()).thenReturn(true); HttpResponse httpResponse2 = mock(HttpResponse.class); when(httpResponse2.asString()).thenReturn("{\"pong\"}"); when(httpResponse2.isSuccess()).thenReturn(true); HttpResponse httpResponse3 = mock(HttpResponse.class); when(httpResponse3.asString()).thenReturn(null); when(httpResponse3.isSuccess()).thenReturn(false); HttpClient client = mock(HttpClient.class); when(client.execute(any(HttpRequest.class))).thenReturn(httpResponse1) .thenReturn(httpResponse2).thenReturn(httpResponse3); RemoteAgent agent = new RemoteAgent("http: assertTrue(agent.ping()); assertFalse(agent.ping()); assertFalse(agent.ping()); }