IterableObjectInputStream implements Iterable<T> { @Override public Iterator<T> iterator() { return inputStream == null ? Collections.emptyIterator() : new ObjectInputStreamIterator(); } IterableObjectInputStream(ObjectInputStream<T> inputStream); @Override Iterator<T> iterator();  }
@Test public void nextClosesTheStreamWhenExhausted() { Iterator<Object> subjectIterator = subject.iterator(); for (int i = 0; i < streamSize; i++) { assertThat(subjectIterator.next(), is(not(nullValue()))); } assertThat(subjectIterator.next(), is(nullValue())); verify(spyInputStream, atLeastOnce()).close(); }
@Test public void callToIteratorConsumesObjectFromStream() { for (int i = 0; i < streamSize; i++) { assertThat(subject.iterator().hasNext(), is(true)); } assertThat(subject.iterator().hasNext(), is(false)); }