I have a list with Strings:
final List<String> serialNumbers=new ArrayList<String>();
for(int i=0;i<numberOfPieces;i++){
serialNumbers.add(Integer.toString(i));
}
Now I want my MockObject to return the next element in the List as a String everytime the mock method is called, how do I do this?
context.checking(new Expectations(){{
exactly(numberOfPieces).of (myMock).getSerialNumber();
// will return serialNumbers i-th element
}});
Solution: use onConsecutiveCalls
as it can take an Array of Actions.
final ReturnValueAction[] serialNumbers=new ReturnValueAction[numberOfPieces];
for(int i=0;i<numberOfPieces;i++){
serialNumbers[i]=new ReturnValueAction(Integer.toString(i));
}
context.checking(new Expectations(){{
exactly(numberOfPieces).of (myMock).getSerialNumber();
will(onConsecutiveCalls(serialNumbers));
}});