AutoCloseables { public static <T> void closeQuietly(@Nullable final Iterable<T> iterable) { if (iterable != null) { iterable.forEach(AutoCloseables::closeQuietly); } } private  AutoCloseables(); static void closeQuietly(@Nullable final Iterable<T> iterable); static void closeQuietly(@Nullable final Object object); static void closeQuietly(@Nullable final AutoCloseable closeable);  }
@Test public void closeQuietly_anAutoCloseable_close() throws Exception { AutoCloseable closeable = mock(AutoCloseable.class); AutoCloseables.closeQuietly(closeable); verify(closeable).close(); }
@Test public void closeQuietly_autoCloseableThrows_doNotPropagate() throws Exception { AutoCloseable closeable = mock(AutoCloseable.class); doThrow(IllegalStateException.class).when(closeable).close(); catchException(() -> AutoCloseables.closeQuietly(closeable)); assertThat("Expected no exception to be thrown", caughtException(), nullValue()); }
@Test public void closeQuietly_nullInstance_noException() throws Exception { final AutoCloseable closeable = null; catchException(() -> AutoCloseables.closeQuietly(closeable)); assertThat("Expected no exception to be thrown", caughtException(), nullValue()); }
@Test public void closeQuietly_anObjectImplementingAutoCloseable_close() throws Exception { AutoCloseable closeable = mock(AutoCloseable.class); Object anyObject = closeable; AutoCloseables.closeQuietly(anyObject); verify(closeable).close(); }
@Test public void closeQuietly_anObjectNotImplementingAutoCloseable_close() throws Exception { Object anyObject = new Object(); catchException(() -> AutoCloseables.closeQuietly(anyObject)); assertThat("Expected no exception to be thrown", caughtException(), nullValue()); }