22

They both seem to do the same thing - why would you use one in preference to the other?

org.mockito.Mockito.stub()
org.mockito.Mockito.mock()
Tom
  • 2,416
  • 3
  • 31
  • 38
  • 13
    the stub method is deprecated, check the docs. The new way of stubbing a method call is what Andy shows. – Augusto Mar 10 '11 at 14:47
  • ah ok - thanks, that makes more sense! – Tom Mar 10 '11 at 17:20
  • @Augusto any idea why `stub` was deprecated? I'm just curious what the key differences are. I tried to find something on the project's mailing list but it's not that easy. – toniedzwiedz Jan 08 '15 at 17:18
  • @toniedzwiedz I think the core idea of the decision is somehow explained in Martin Fowler's blog post [Mocks aren't Stubs](http://martinfowler.com/articles/mocksArentStubs.html). This is not really related to Mockito, but I have the assumption that quite a few of the big brains of TDD were discussing what is a stub or a mock. This obviously is a **big** assumption, it would be interesting to see the dev discussion list / commit history to get a more factual reason :). – Augusto Jan 11 '15 at 06:15
  • "They are technically the same thing" - from one of the core Mockito committers: http://stackoverflow.com/a/14737131/377384 – lindon fox Oct 27 '15 at 00:48

1 Answers1

16

You can use a mock object to verify that you have called it in the way expected. In Mockito, mocked objects are automatically stubs, and verification occurs explicitly.

From Mockito's "Why do we need another mocking framework?":

 Separation of stubbing and verification. Should let me code in line with intuition: 
 stub before execution, selectively verify interactions afterwards. I don’t 
 want any verification-related code before execution.

You can stub the behavior of calls before they're called. For example (from the Mockito home page):

 when( mockedList.get(0)).thenReturn( "first" );

You can verify interactions with mocked objects after they're called. For example:

 verify( mockedList ).add("one");
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • sorry - i meant the methods, I have updated title and question. – Tom Mar 10 '11 at 14:43
  • 1
    I've accepted this answer as it is useful info but the real answer to my question is that given by Augusto - stub() is deprecated. – Tom Mar 10 '11 at 17:20