Попробуйте сделать итератор final
и использовать его в ответе
private void testCase(String config, String orders, List<String> outputs){
try {
CoffeeMachineRunner machineRunner = new CoffeeMachineRunner();
when(inputProvider.getConfigInput()).thenReturn(config);
List<Order> orderList = getOrderFromJson(orders);
final Iterator<Order> iter = orderList.iterator();
doAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return iter.hasNext();
}
}).when(inputProvider).hasNextInput();
doAnswer(new Answer<Order>() {
@Override
public Order answer(InvocationOnMock invocation) throws Throwable {
return iter.next();
}
}).when(inputProvider).getNextInput();
machineRunner.simulate(inputProvider);
assertStringIn(outputs, outContent.toString());
} catch (Exception e) {
assertTrue(false);
}
}
From JDK-8 + с использованием Lambda
private void testCase(String config, String orders, List<String> outputs){
try {
CoffeeMachineRunner machineRunner = new CoffeeMachineRunner();
when(inputProvider.getConfigInput()).thenReturn(config);
List<Order> orderList = getOrderFromJson(orders);
final Iterator<Order> iter = orderList.iterator();
doAnswer(invocation -> iter.hasNext()).when(inputProvider).hasNextInput();
doAnswer(invocation -> iter.next()).when(inputProvider).getNextInput();
machineRunner.simulate(inputProvider);
assertStringIn(outputs, outContent.toString());
} catch (Exception e) {
assertTrue(false);
}
}