### Question:
HttpUtils { public static void consumeAndClose(@Nullable HttpURLConnection connection) { if (connection != null) { IOUtils.consumeAndClose(connection.getErrorStream()); try { IOUtils.consumeAndClose(connection.getInputStream()); } catch (IOException ignored) { } } } private  HttpUtils(); static String readToString(final InputStream inputStream); static void consumeAndClose(@Nullable HttpURLConnection connection);  }### Answer:
@Test void consumeAndCloseIgnoresNullConnection() { HttpUtils.consumeAndClose(null); }
@Test void consumeAndCloseNoStreams() throws IOException { HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getErrorStream()).thenReturn(null); when(connection.getInputStream()).thenReturn(null); HttpUtils.consumeAndClose(connection); }
@Test void consumeAndCloseException() throws IOException { HttpURLConnection connection = mock(HttpURLConnection.class); InputStream errorStream = mockEmptyInputStream(); when(connection.getErrorStream()).thenReturn(errorStream); when(connection.getInputStream()).thenThrow(IOException.class); HttpUtils.consumeAndClose(connection); verify(errorStream).close(); }
@Test void consumeAndCloseResponseContent() throws IOException { HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getErrorStream()).thenReturn(null); InputStream responseStream = mockEmptyInputStream(); when(connection.getInputStream()).thenReturn(responseStream); HttpUtils.consumeAndClose(connection); verify(responseStream).close(); }