/** * Returns elements of the collection. Keeps returning the last element forever. * Might be useful on occasion when you have a collection of elements to return. * <p> * <pre class="code"><code class="java"> * //this: * when(mock.foo()).thenReturn(1, 2, 3); * * //is equivalent to: * when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3))); * </code></pre> * * @param elements The collection of elements to return. * @return the answer * * @since 1.9.5 */ public static <T> Answer<T> returnsElementsOf(Collection<?> elements) { return (Answer<T>) new ReturnsElementsOf(elements); }
@Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { return returnsElementsOf.answer(invocationOnMock); } }
/** * Returns elements of the collection. Keeps returning the last element forever. * Might be useful on occasion when you have a collection of elements to return. * <p> * <pre class="code"><code class="java"> * //this: * when(mock.foo()).thenReturn(1, 2, 3); * * //is equivalent to: * when(mock.foo()).thenReturn(new ReturnsElementsOf(Arrays.asList(1, 2, 3))); * </code></pre> * * @param elements The collection of elements to return. * @return the answer * * @since 1.9.5 */ public static <T> Answer<T> returnsElementsOf(Collection<?> elements) { return (Answer<T>) new ReturnsElementsOf(elements); } }
public ChainReturns(Object toBeReturn, Object... toBeReturnedOthers) { List<Object> elements = new LinkedList<Object>(); elements.add(toBeReturn); addOtherElementToBeReturned(elements, toBeReturnedOthers); this.returnsElementsOf = new ReturnsElementsOf(elements); }
List<String[]> returnList = new ArrayList<>(); public CSVReaderMock returnLines(@NonNull List<String> lines) { try { for (String line : lines) { String[] lineArr = null; if (line != null) { lineArr = line.split(","); } returnList.add(lineArr); } linesCorrectInitialized = true; } catch (IOException e) { /* ... */ }; return this; } public CSVReader create() { if (!linesCorrectInitialized) { /* ... */ } // Return null repeatedly after all stubs are exhausted. returnList.add(null); when(reader.readNext()).thenAnswer(new ReturnsElementsOf(returnList)); return reader; }