WrappedCloseableIterator implements CloseableIterator<T> { @Override public void close() { CloseableUtil.close(iterator); } WrappedCloseableIterator();  WrappedCloseableIterator(final Iterator<T> iterator); @Override void close(); @Override boolean hasNext(); @Override T next(); @Override void remove();  }
@Test public void shouldDelegateCloseToWrappedIterator() { final CloseableIterator<Object> closeableIterator = mock(CloseableIterator.class); final WrappedCloseableIterator<Object> wrappedIterator = new WrappedCloseableIterator<>(closeableIterator); wrappedIterator.close(); verify(closeableIterator).close(); }
@Test public void shouldDoNothingWhenCloseCalledOnNoncloseableIterator() { final Iterator<Object> iterator = mock(Iterator.class); final WrappedCloseableIterator<Object> wrappedIterator = new WrappedCloseableIterator<>(iterator); assertDoesNotThrow(() -> wrappedIterator.close()); }