Issue
when(/* some method call*/).thenReturn(mockFetchReturn).thenReturn(mockFetchReturn2)
.thenReturn(mockFetchReturn3);
This is working fine and I am able to call mocked method three times with different output. But my output list can change for each test scenario and I couldn't find how this can be done in a loop based on different returns. For e.g. If I pass a list of 10 mockFetchReturn3 objects then there should be 10 return statements.
Solution
Just code for the answer provided in comment:
OngoingStubbing stubbing = when(/* some method call*/);
for (int i = 0; ...) {
stubbing = stubbing.thenReturn(mockFetchReturn(i));
}
Alternatively, you can pass a list to
List<String> answers = Arrays.asList(mockFetchReturn, mockFetchReturn, ...);
when(/* some method call*/).thenAnswer(AdditionalAnswers.returnsElementsOf(logEntryList));
Also see similar questions
Answered By - tkruse