SuppliedIterable implements CloseableIterable<T> { @Override public CloseableIterator<T> iterator() { final Iterable<T> iterable = supplier.get(); closeables.add(iterable); return new WrappedCloseableIterable<>(iterable).iterator(); } SuppliedIterable(final Supplier<? extends Iterable<T>> supplier); @Override CloseableIterator<T> iterator(); @Override void close();  }
@Test public void shouldRequestNewIterableFromSupplierWhenIteratorInvoked() { final Supplier<Iterable<Integer>> supplier = mock(Supplier.class); final Iterable<Integer> iterable1 = Arrays.asList(1, 2, 3); final Iterable<Integer> iterable2 = Arrays.asList(4, 5, 6); given(supplier.get()).willReturn(iterable1, iterable2); final SuppliedIterable<Integer> suppliedItr = new SuppliedIterable<>(supplier); final CloseableIterator<Integer> result1 = suppliedItr.iterator(); final CloseableIterator<Integer> result2 = suppliedItr.iterator(); verify(supplier, times(2)).get(); assertEquals(iterable1, Lists.newArrayList(result1)); assertEquals(iterable2, Lists.newArrayList(result2)); }