@Test(expected = ConcurrentModificationException.class) public void testNextWhenIteratorHasLessElementsThanExpected() { List<Integer> list = Arrays.asList(1, 2, 3); Iterator<Integer> iter = new IteratorWithConcurrentModificationChecker<>(list.iterator(), 4, "Exception"); assertEquals(Integer.valueOf(1), iter.next()); assertEquals(Integer.valueOf(2), iter.next()); assertEquals(Integer.valueOf(3), iter.next()); iter.next(); }
@Override public T next() { try { T next = delegate.next(); if (expCnt == 0) throw new ConcurrentModificationException(eMsg); expCnt--; return next; } catch (NoSuchElementException e) { if (expCnt == 0) throw e; else throw new ConcurrentModificationException(eMsg); } }
IteratorWithConcurrentModificationChecker implements Iterator<T> { @Override public T next() { try { T next = delegate.next(); if (expCnt == 0) throw new ConcurrentModificationException(eMsg); expCnt--; return next; } catch (NoSuchElementException e) { if (expCnt == 0) throw e; else throw new ConcurrentModificationException(eMsg); } } }
IteratorWithConcurrentModificationChecker implements Iterator<T> { @Override public T next() { try { T next = delegate.next(); if (expCnt == 0) throw new ConcurrentModificationException(eMsg); expCnt--; return next; } catch (NoSuchElementException e) { if (expCnt == 0) throw e; else throw new ConcurrentModificationException(eMsg); } } IteratorWithConcurrentModificationChecker(Iterator<T> delegate, long expCnt, String eMsg); }
IteratorWithConcurrentModificationChecker implements Iterator<T> { @Override public T next() { try { T next = delegate.next(); if (expCnt == 0) throw new ConcurrentModificationException(eMsg); expCnt--; return next; } catch (NoSuchElementException e) { if (expCnt == 0) throw e; else throw new ConcurrentModificationException(eMsg); } } IteratorWithConcurrentModificationChecker(Iterator<T> delegate, long expCnt, String eMsg); @Override boolean hasNext(); @Override T next(); }
IteratorWithConcurrentModificationChecker implements Iterator<T> { @Override public T next() { try { T next = delegate.next(); if (expCnt == 0) throw new ConcurrentModificationException(eMsg); expCnt--; return next; } catch (NoSuchElementException e) { if (expCnt == 0) throw e; else throw new ConcurrentModificationException(eMsg); } } IteratorWithConcurrentModificationChecker(Iterator<T> delegate, long expCnt, String eMsg); @Override boolean hasNext(); @Override T next();  }