0

As per THIS post, There are two ways to mock the method doSomeStuff() to return a 1 :

when(bloMock.doSomeStuff()).thenReturn(1);

and

doReturn(1).when(bloMock).doSomeStuff();

The very important difference is that the first option will actually call the doSomeStuff()- method while the second will not

So, my question is what is the point in having the first option which actually calls the actual method but returns 1 only. In which use case, we may want to something like that?

Andy897
  • 6,915
  • 11
  • 51
  • 86
  • 2
    Possible duplicate: http://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when – troig Apr 06 '16 at 06:43
  • 1
    Actually I carefully read your question. However, I never heard before the difference you menction (and I just noticed that's described in one of the answers of my linked question). Anyway, I'll be waiting for answers. Thanks for comment :) – troig Apr 06 '16 at 08:13
  • 2
    Also think it's a duplicate, your question `what is the point in having the first option` is answered in the accepted answer: `One thing that when/thenReturn gives you, that doReturn/when doesn't, is type-checking of the value that you're returning, at compile time.` – Bewusstsein Apr 06 '16 at 11:44
  • 1
    @BretC If `bloMock` is a spy, it would indeed call the actual method, see http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#13. As for the discussion whether this is a duplicate or not, I agree that it is. I suspect that `stub()` (which was refactored to `when()`) existed before `toReturn()` (which was refactored to `thenReturn()`), and that they kept both ways of mocking. I myself prefer the first option in the question, and only use `doReturn()` when using spies or mocking void methods. – Magnilex Apr 06 '16 at 16:13
  • 1
    This question reads very much like [this other one here](http://stackoverflow.com/questions/36343149/mockito-difference-between-dothrow-and-thenthrow/36343419), for which the answer is the same: `when` reads like a grammatical sentence in English, and in the case of mocks (not spies) there is no reason not to use the more readable style. – Jeff Bowman Apr 06 '16 at 22:08

1 Answers1

1

I dug a bit more than this, and the answer to why both syntaxes exist can be found in the old release notes, and a referenced mailing list discussion.

To start with, doReturn() was added in version 1.5 (26/07/2008), while when() was added in version 1.6 (21/10/2008). when() was implemented to replace the old stub() method and doReturn() to replace stubVoid(). Basically this is a design deicision by the creator of Mockito (cited from the mailing list 29/06/2008):

I never liked stubVoid() syntax but that was the best I could think of. The stubbing syntax I'd implement now if I did mockito again:

//regular stubbing:

when(mock.getStuff()).thenReturn("foo");
when(mock.getStuff()).thenThrow(new RuntimeException());

//for void methods and some corner cases:

doReturn("foo").when(mock).getStuff();
doThrow(new RuntimeException()).when(mock).someMethod();

//for stubbing consecutively:

when(mock.getStuff()).thenReturn("foo").thenThrow(new RuntimeException());
doThrow(new RuntimeException()).thenReturn("foo").when(mock).someMethod();

I proposed this syntax couple of weeks ago but received only single feedback saying that it's rather cosmetic (which is true...). Hence I decided not to change the API.

And as already pointed out by Bewusstsein in the comments, when() provides type safety. If we have a method String doSomething() both below blocks will compile. The latter will however throw an exception during runtime.

Mockito.doReturn("String").when(mock).doSomething();
Mockito.doReturn(1).when(mock).doSomething();

So, to conclude, it was a design decision to introduce both ways of mocking. when() was imlemented as the prefered way of mocking, due to its type safety and its fluent reading. doReturn() was implemented to allow for mocking of void methods and other corner cases.

Community
  • 1
  • 1
Magnilex
  • 11,584
  • 9
  • 62
  • 84