### Question:
RestMetricsSender implements MetricsSender { @Override public String pushMetrics(String payload) { String responseString = ""; UrlService svc = null; Stopwatch timer = Stopwatch.createStarted(); try { LOG.info("server: {}", collectorServiceAddress); svc = getConnectedUrlService(); responseString = svc.send(payload); timer.stop(); LOG.info("http response time: " + timer.elapsed(TimeUnit.MILLISECONDS) + " ms"); if (responseString.length() > 0) { LOG.debug("POST response from server: " + responseString); } } catch (IOException e) { LOG.error("", e); } finally { if (svc != null) { svc.disconnect(); } } return responseString; } RestMetricsSender(String metricsHost); @Override String pushMetrics(String payload);  }### Answer:
@Test public void testPushMetrics() throws Exception { final UrlService svcMock = createStrictMock(UrlService.class); final String payload = "test"; final String expectedResponse = "mockResponse"; expect(svcMock.send(anyString())).andReturn(expectedResponse); svcMock.disconnect(); expectLastCall(); replay(svcMock); RestMetricsSender sender = new RestMetricsSender("expectedHostName") { @Override protected UrlService getConnectedUrlService() throws IOException { return svcMock; } }; String response = sender.pushMetrics(payload); verify(svcMock); assertEquals("", expectedResponse, response); }
@Test public void testPushMetricsFailed() throws Exception { final UrlService svcMock = createStrictMock(UrlService.class); final String payload = "test"; final String expectedResponse = "mockResponse"; RestMetricsSender sender = new RestMetricsSender("expectedHostName") { @Override protected UrlService getConnectedUrlService() throws IOException { return svcMock; } }; expect(svcMock.send(anyString())).andThrow(new IOException()); svcMock.disconnect(); expectLastCall(); replay(svcMock); String response = sender.pushMetrics(payload); verify(svcMock); }