### Question:
CyclicIterator implements Iterator<T> { @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } if (iteratorContainsMoreElements()) { T next = iterator.next(); elements.add(next); return next; } else { T next = elements.get(index); if (index == (elements.size() - 1)) { if (!shouldCycleInfinitely()) { repeats++; } index = 0; } else { index++; } return next; } } CyclicIterator(Iterator<? extends T> iterator);  CyclicIterator(Iterator<? extends T> iterator, int numberOfTimesToRepeat); @Override boolean hasNext(); @Override T next(); @Override void remove(); @Override String toString();  }### Answer:
@Test(expected = NoSuchElementException.class) public void shouldThrowNoSuchElementExceptionIfNextIsCalledAndANumberOfRepeatsOfZeroIsSpecified() throws Exception { Iterable<Integer> input = iterableWith(1, 2); CyclicIterator<Integer> iterator = new CyclicIterator<Integer>(input.iterator(), 0); iterator.next(); }
@Test(expected = NoSuchElementException.class) public void shouldThrowNoSuchElementExceptionIfNextIsCalledAndAnEmptyIteratorIsSupplied() throws Exception { Iterable<Integer> input = iterableOf(Integer.class); CyclicIterator<Integer> iterator = new CyclicIterator<Integer>(input.iterator()); iterator.next(); }
@Test public void shouldAllowNextToBeCalledWithoutCallingHasNextFirst() throws Exception { Iterable<Integer> input = iterableWith(1, 2, 3); CyclicIterator<Integer> iterator = new CyclicIterator<Integer>(input.iterator()); assertThat(iterator.next(), is(1)); assertThat(iterator.next(), is(2)); assertThat(iterator.next(), is(3)); assertThat(iterator.next(), is(1)); assertThat(iterator.next(), is(2)); assertThat(iterator.next(), is(3)); }
@Test public void shouldAllowNullValuesInTheInputIterator() throws Exception { Iterator<String> input = iterableWith("a", "b", null).iterator(); Iterator<String> iterator = new CyclicIterator<String>(input); assertThat(iterator.next(), is("a")); assertThat(iterator.next(), is("b")); assertThat(iterator.next(), is(nullValue())); }