0

Is it possible to set different expectations for a mock which is being invoked in a loop?

Considering the use case,

while(condition){
   List list = Database call //Call to be mocked
   ....
   Logic based on above response
}

how can we set expectations such the first iteration returns a list of objects, while the second call returns empty list?

ram
  • 747
  • 2
  • 11
  • 34

1 Answers1

2

Try this:

expect(mockDatabase.call()).andReturn(object1).once();
expect(mockDatabase.call()).andReturn(object2).once();
replay(mockDatabase);
iluxa
  • 6,941
  • 18
  • 36