I have an object whose method functionality I want to replace by just incrementing a counter when its called. The method has a void
return type though, so I've been looking into how to stub void methods using mockito, but have not found a solution that works. From what I read I thought I could do something like this:
int count = 0;
Mockito.doReturn(++count).when(mockObject).mockObjectMethod(Mockito.anyString());
When I run this, I get
org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue
So I believe I found two solutions: use doThrow
, where I could catch the exception and increment the counter OR use toAnswer
and increment the counter in the implementation of the Answer
. Are there other solutions?