IOUtilities { public static void closeSilently(final Closeable closeable) { if (closeable == null) { return; } try { closeable.close(); } catch (IOException ignored) { } } private  IOUtilities(); static void closeSilently(final Closeable closeable); static void copyStreams(final InputStream in, final OutputStream out); static void copyStreams(final InputStream in, final OutputStream out, final int bufferSize);  }
@Test public void shouldCloseClosableSilently() throws IOException { Closeable closeable = mock(Closeable.class); doThrow(new IOException("This should never happen")).when(closeable).close(); try { closeable.close(); fail("IOException should be thrown."); } catch (Exception e) { } IOUtilities.closeSilently(closeable); }
@Test public void shouldCloseNullClosableSilently() { try { IOUtilities.closeSilently(null); } catch (Exception e) { fail("Exception should not be thrown."); } }