FibonacciIterator implements Iterator<Long> { @Override public Long next() { if (iteration == 0) { previousValues[0] = 0; previousValues[1] = 0; } else if (iteration == 1) { previousValues[0] = 0; previousValues[1] = 1; } long result = previousValues[0] + previousValues[1]; if (iteration > 1) { previousValues[0] = previousValues[1]; previousValues[1] = result; } iteration++; return result; } FibonacciIterator(); @Override boolean hasNext(); @Override Long next(); @Override void remove();  }
@Test public void shouldGenerateFibonacciSequence() { FibonacciIterator fibonaccis = new FibonacciIterator(); assertThat(fibonaccis.next(), is(0L)); assertThat(fibonaccis.next(), is(1L)); assertThat(fibonaccis.next(), is(1L)); assertThat(fibonaccis.next(), is(2L)); assertThat(fibonaccis.next(), is(3L)); assertThat(fibonaccis.next(), is(5L)); assertThat(fibonaccis.next(), is(8L)); assertThat(fibonaccis.next(), is(13L)); assertThat(fibonaccis.next(), is(21L)); assertThat(fibonaccis.next(), is(34L)); assertThat(fibonaccis.next(), is(55L)); assertThat(fibonaccis.next(), is(89L)); assertThat(fibonaccis.next(), is(144L)); }