PeekIterator implements Iterator<T> { public T peek() { if (!hasNext()) { throw (new NoSuchElementException("Iterator has no elements left.")); } return nextitem; } PeekIterator(Iterator<T> iterator); @Override boolean hasNext(); @Override T next(); T peek(); @Override void remove();  }
@Test public void peek_ThrowsExceptionWithEmptyList() { PeekIterator<Integer> tested = new PeekIterator<Integer>( emptyList.iterator()); boolean thrown = false; try { tested.peek(); } catch (NoSuchElementException e) { thrown = true; } assertTrue(String.format("Exception: %s not thrown", NoSuchElementException.class.getCanonicalName()), thrown); }