In my unit test I need to mock an interface which among different methods has nextItem() and isEmpty() methods:
public interface MyQueue {
Item nextItem();
boolean isEmpty();
//other methods
...
}
My requirement for the mock is that isEmpty() initially should return false, but after nextItem() was called isEmpty() should return true. Thus I’m mocking a queue with one item.
- What is the simplest way to implement this kind of mock with mockito?
- Can I implement additional requirement: calling
nextItem()second, third time and so on will result in a specific kind of exception?
P.S. I don’t want to provide the full implementation of my interface for the test, because of other methods in it, resulting in hard-to-understand and verbose code.
You can achieve that with thenAnswer(), a feature Mockito documentation sees as controversial:
Yet another controversial feature which was not included in Mockito originally. We recommend using simple stubbing with toReturn() or toThrow() only. Those two should be just enough to test/test-drive any clean & simple code.
Here’s thenAnswer: