CloseableUtil { public static void close(final Object... objs) { for (final Object obj : objs) { close(obj); } } private  CloseableUtil(); static void close(final Object... objs); static void close(final Object obj); static void close(final AutoCloseable... closeable); static void close(final AutoCloseable closeable);  }
@Test public void shouldCloseACloseable() throws IOException { final Closeable closeable = mock(Closeable.class); CloseableUtil.close(closeable); verify(closeable).close(); }
@Test public void shouldCloseAllCloseables() throws IOException { final Closeable closeable1 = mock(Closeable.class); final Closeable closeable2 = mock(Closeable.class); final Object nonCloseable = mock(Object.class); CloseableUtil.close(closeable1, nonCloseable, closeable2); verify(closeable1).close(); verify(closeable2).close(); }
@Test public void shouldNotThrowExceptionForNullObject() { assertDoesNotThrow(() -> CloseableUtil.close((Object) null)); }
@Test public void shouldNotThrowExceptionForNonCloseableObject() { assertDoesNotThrow(() -> CloseableUtil.close("Some string")); }