title: Stub void method using mockito
body: <p>I have an object whose method functionality I want to replace by just incrementing a counter when its called. The method has a <code>void</code> 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:</p>
  
  <pre><code>int count = 0;
  Mockito.doReturn(++count).when(mockObject).mockObjectMethod(Mockito.anyString());
  </code></pre>
  
  <p>When I run this, I get </p>
  
  <blockquote>
    <p>org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue</p>
  </blockquote>
  
  <p>So I believe I found two solutions: use <code>doThrow</code>, where I could catch the exception and increment the counter OR use <code>toAnswer</code> and increment the counter in the implementation of the <code>Answer</code>. Are there other solutions?</p>