315

I am currently in the process of using Mockito to mock my service layer objects in a Spring MVC application in which I want to test my Controller methods. However, as I have been reading on the specifics of Mockito, I have found that the methods doReturn(...).when(...) is equivalent to when(...).thenReturn(...). So, my question is what is the point of having two methods that do the same thing or what is the subtle difference between doReturn(...).when(...) and when(...).thenReturn(...)?

Any help would be appreciated.

2
  • 2
    The javadoc has a few cases where doReturn() is useful. Dec 3, 2013 at 14:39
  • 7
    I think one of the main difference is that doReturn(...).when(..) is an older one and its not that type safe and hence we can use it sometimes when the compiler keeps complaining about casting. The when(..).thenReturn(..) is much better in terms of type safety Dec 3, 2013 at 14:45

5 Answers 5

325

The two syntaxes for stubbing are roughly equivalent. However, you can always use doReturn/when for stubbing; but there are cases where you can't use when/thenReturn. Stubbing void methods is one such. Others include use with Mockito spies, and stubbing the same method more than once.

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. However, I believe this is of almost no value - if you've got the type wrong, you'll find out as soon as you run your test.

I strongly recommend only using doReturn/when. There is no point in learning two syntaxes when one will do.

You may wish to refer to my answer at Forming Mockito "grammars" - a more detailed answer to a very closely related question.

12
  • 34
    I kinda disagree with David. I often run into situations where I return the wrong type when using doReturn/when and spend the next few minutes figuring out what went wrong. Compile type type checking becomes extremely useful with when/thenReturn.
    – Saket
    Feb 15, 2016 at 13:14
  • 26
    Just keep in mind that Mockito recommends you use use when/thenReturn instead of doReturn/when.
    – CodyEngel
    Feb 13, 2017 at 21:42
  • 5
    @CodyEngel and there is no reason for such a recommendation, other than what I've outlined in my answers here and at stackoverflow.com/q/11462697. Several years ago, I discussed this with Brice Dutheil, who is currently acting lead developer of Mockito, and to the best of my memory, he agrees. I will ask him to post a comment here (no guarantee that he'll do so). Feb 13, 2017 at 21:50
  • 35
    The javadoc states that doReturn/when is a trade-off. The team does not recommand one way or another but note the when/then approach is more intuitive, more readable and offer compile time check, it is the approach that made Mockito popular and easy to use, don't forget that when the code base is shared by various skillset in your team ; yet it has drawbacks regarding spies and void methods.
    – bric3
    Feb 24, 2017 at 11:11
  • 9
    Just for the record: doReturn() has the big disadvantage of turning into YODA style coding of method calls. The thing later comes written down first that is. Most people read left to right; so you now have to constantly remember to reverse the return-when logic in your head.
    – GhostCat
    Feb 24, 2017 at 13:40
324

Both approaches behave differently if you use a spied object (annotated with @Spy) instead of a mock (annotated with @Mock):

  • when(...) thenReturn(...) makes a real method call just before the specified value will be returned. So if the called method throws an Exception you have to deal with it / mock it etc. Of course you still get your result (what you define in thenReturn(...))

  • doReturn(...) when(...) does not call the method at all.

Example:

public class MyClass {
     protected String methodToBeTested() {
           return anotherMethodInClass();
     }

     protected String anotherMethodInClass() {
          throw new NullPointerException();
     }
}

Test:

@Spy
private MyClass myClass;

// ...

// would work fine
doReturn("test").when(myClass).anotherMethodInClass();

// would throw a NullPointerException
when(myClass.anotherMethodInClass()).thenReturn("test");
9
  • 64
    This behavior just works for spied objects, since are "wrapper" of real objects. In the case of mocked objects, it does not matter if it's when/thenReturn or doReturn/ when. Mocked objects never calls real methods. Sep 11, 2015 at 19:43
  • Could you please give more information why do we need use this functionality? I don't see practical use case. The intent of test is confirm correctness of the code under different use cases. If the calll of the method throws exception test should throw exception, not return a value
    – Gleichmut
    Jun 7, 2017 at 3:19
  • @Gleichmut This was a hypothetical scenario, where i show the usage/advantage of doReturn. In a real application, a method which just returns an exception makes of course no sense.. but you have methods (probably not so thin like this one) which may throw exceptions in certain conditions..
    – akcasoy
    Jun 30, 2017 at 7:52
  • 2
    Just for clarification: The when().thenReturn()-method calls the actual method (of a spy - doesn't matter for mocks) only once. This happens in the line you specify the mock behaviour (when(myClass.anotherMethodInClass().thenRet...). After that the actual method is never called again. Maybe good to know if you did expect some decorator logic when reading the explanation above.
    – Jonas
    Dec 17, 2018 at 6:34
  • This doesn't seem like an advantage of doReturn(), it looks like an abuse of the library. The point of spying instead of pure mocking is to take advantage of real calls. They also warn against using Spies like this: github.com/mockito/mockito/wiki/Using-Spies-(and-Fakes) (and recommend extending the class and overriding the method instead) Jan 16, 2019 at 17:56
24

The Mockito javadoc seems to tell why use doReturn() instead of when() Use doReturn() in those rare occasions when you cannot use Mockito.when(Object).

Beware that Mockito.when(Object) is always recommended for stubbing because it is argument type-safe and more readable (especially when stubbing consecutive calls).

Here are those rare occasions when doReturn() comes handy:

1. When spying real objects and calling real methods on a spy brings side effects

List list = new LinkedList(); List spy = spy(list);

//Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)

when(spy.get(0)).thenReturn("foo");

//You have to use doReturn() for stubbing: doReturn("foo").when(spy).get(0);

2. Overriding a previous exception-stubbing:

when(mock.foo()).thenThrow(new RuntimeException());

//Impossible: the exception-stubbed foo() method is called so RuntimeException is thrown. when(mock.foo()).thenReturn("bar");

//You have to use doReturn() for stubbing:

doReturn("bar").when(mock).foo(); Above scenarios shows a tradeoff of Mockito's elegant syntax. Note that the scenarios are very rare, though. Spying should be sporadic and overriding exception-stubbing is very rare. Not to mention that in general overridding stubbing is a potential code smell that points out too much stubbing.

12

Continuing this answer, There is another difference that if you want your method to return different values for example when it is first time called, second time called etc then you can pass values so for example...

PowerMockito.doReturn(false, false, true).when(SomeClass.class, "SomeMethod", Matchers.any(SomeClass.class));

So it will return false when the method is called in same test case and then it will return false again and lastly true.

1
  • This is also possible with when(...).thenReturn(...) - you can supply multiple arguments to the thenReturn() and it will do the same thing.
    – Bernie
    Jan 24 at 22:09
7

The latter alternative is used for methods on mocks that return void.

Please have a look, for example, here: How to make mock to void methods with mockito

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.