LazyIterator implements Iterator<T> { @Override public final boolean hasNext() { if (state == State.FAILED) { throw new IllegalStateException("This iterator is in an inconsistent state, and can no longer be used, " + "due to an exception previously thrown by the computeNext() method"); } switch (state) { case DONE: return false; case READY: return true; } return tryToComputeNext(); }  @Override void remove(); @Override final boolean hasNext(); @Override final T next();  }
@Test public void testHasNext_IllegalState() { LazyIterator<Integer> lazyIterator = newFaultyLazyIterator(); try { lazyIterator.hasNext(); } catch (RuntimeException expected) { } try { lazyIterator.hasNext(); Assert.fail("should throw IllegalStateException on second call to hasNext, if previous call failed"); } catch (IllegalStateException expected) { } }