forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMocking.java
More file actions
32 lines (24 loc) · 944 Bytes
/
Mocking.java
File metadata and controls
32 lines (24 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package testsupport;
import org.mockito.stubbing.Answer;
import java.util.Iterator;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class Mocking {
@SuppressWarnings("unchecked")
public static <A> Iterable<A> mockIterable() {
Iterable<A> iterable = (Iterable<A>) mock(Iterable.class);
when(iterable.iterator()).thenReturn((Iterator<A>) mock(Iterator.class));
return iterable;
}
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> void mockIteratorToHaveValues(Iterator<?> iterator, T... values) {
Iterator<?> real = asList(values).iterator();
when(iterator.hasNext()).then(delegateTo(real));
when(iterator.next()).then(delegateTo(real));
}
public static Answer<?> delegateTo(final Object real) {
return invocation -> invocation.getMethod().invoke(real);
}
}