Skip to content

Instantly share code, notes, and snippets.

@stevez
Created May 7, 2015 02:30
Show Gist options
  • Save stevez/991c8685d68fc8af476a to your computer and use it in GitHub Desktop.
Save stevez/991c8685d68fc8af476a to your computer and use it in GitHub Desktop.
mock for loop using mockito
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collection;
import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
public class TestMockedIterator {
private Collection<String> fruits;
private Iterator<String> fruitIterator;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
fruitIterator = mock(Iterator.class);
when(fruitIterator.hasNext()).thenReturn(true, true, true, false);
when(fruitIterator.next()).thenReturn("Apple")
.thenReturn("Banana").thenReturn("Pear");
fruits = mock(Collection.class);
when(fruits.iterator()).thenReturn(fruitIterator);
}
@Test
public void test() {
int iterations = 0;
for (String fruit : fruits) {
iterations++;
}
assertEquals(3, iterations);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment