### Question:
GraphiteMetricSender implements MetricSender { @Override public void send(String name, long value) { send(singletonMap(name, value)); } GraphiteMetricSender(GraphiteSender graphite, Clock clock, String prefix); static GraphiteMetricSender newInstance(String host, String prefix); @Override void send(String name, long value); @Override void send(Map<String, Long> metrics);  }### Answer:
@Test public void typical() throws IOException { when(graphite.isConnected()).thenReturn(false); sender.send("name", 2L); verify(graphite).connect(); verify(graphite).send("prefix.name", "2", 1L); verify(graphite).flush(); verify(graphite).close(); }
@Test public void alreadyConnected() throws IOException { when(graphite.isConnected()).thenReturn(true); sender.send("name", 2L); verify(graphite, never()).connect(); verify(graphite).send("prefix.name", "2", 1L); verify(graphite).flush(); verify(graphite).close(); }
@Test public void exceptionOnConnect() throws IOException { when(graphite.isConnected()).thenReturn(false); doThrow(IOException.class).when(graphite).connect(); try { sender.send("name", 2L); } catch (Exception e) { fail("Unexpected exception"); } verify(graphite, never()).send("prefix.name", "2", 1L); verify(graphite, never()).flush(); verify(graphite).close(); }
@Test public void exceptionOnSend() throws IOException { when(graphite.isConnected()).thenReturn(true); doThrow(IOException.class).when(graphite).send(anyString(), anyString(), anyLong()); try { sender.send("name", 2L); } catch (Exception e) { fail("Unexpected exception"); } verify(graphite, never()).flush(); verify(graphite).close(); }
@Test public void exceptionOnFlush() throws IOException { when(graphite.isConnected()).thenReturn(true); doThrow(IOException.class).when(graphite).flush(); try { sender.send("name", 2L); } catch (Exception e) { fail("Unexpected exception"); } verify(graphite).close(); }
@Test public void exceptionOnClose() throws IOException { when(graphite.isConnected()).thenReturn(true); doThrow(IOException.class).when(graphite).close(); try { sender.send("name", 2L); } catch (Exception e) { fail("Unexpected exception"); } verify(graphite).close(); }