simpler & better mocking

Clone this repo:
  1. 57a3320 Added a TODO. by Szczepan Faber · 10 years ago master
  2. b0a8aef Documented the varargs behavior of argument captor. by Szczepan Faber · 10 years ago
  3. a7751c2 Changed the vargars behavior of argument captor. I prefer if the captor handles the varargs seamlessly, e.g. without any extra methods. The change needs some refactoring and documentation. The previous implementation was nice and had nice coverage - we might want to put that back if our users request extra methods for retrieving varg values from the captor. by Szczepan Faber · 10 years ago
  4. e69d840 Removed the MockitoSpy and MockitoMock interfaces. See issue 429. by Szczepan Faber · 10 years ago
  5. e975352 Made the across-classloader serialization optional. by Szczepan Faber · 10 years ago

Mockito

simplier & better mocking

Current release

06/10/2012 Mockito 1.9.5 released! See the release notes. Should appear in maven cdntral shortly.

Moving to github

We are currently moving a few stuff from Google Code to Github.

For now only the code repository is moved, issue tracker might follow, but other stuff might stay there like the wiki pages and documentation, discussion group will stay at google.

Why drink it?

Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn't give you hangover because the tests are very readable and they produce clean verification errors. Read more about features & motivations.

"We decided during the main conference that we should use JUnit 4 and Mockito because we think they are the future of TDD and mocking in Java" - Dan North, the originator of BDD

More quotes

Over 15000 downloads of 1.9.0 version ('12), excluding maven/Gradle users. For latest figures see the downloads.

More about the user base

How do I drink it?

Download mockito-all-x.x.x.jar and put it on the classpath. If you use a fancy build system with declarative dependencies like Gradle or Maven please -> Click HERE <-

Then you can verify interactions

import static org.mockito.Mockito.*;

// mock creation
List mockedList = mock(List.class);

// using mock object ; observe that it didn't throw any "unexpected interaction exception" exception
mockedList.add("one");
mockedList.clear();

// selective & explicit verification
verify(mockedList).add("one");
verify(mockedList).clear();

Or stub method calls

// you can mock conmcrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);

// stubbing; before the actual execution
when(mockedList.get(0)).thenReturn("first");

// the following prints "first"
System.out.println(mockedList.get(0));

// the following prints "null" because get(999) was not subbed
System.out.println(mockedList.get(999));

You can go further

  • Try the annotations @Mock, @Spy, @Captor, @InjectMocks
  • Try BDD syntax with BDDMockito
  • If the provided answers doesn't fit your needs, write one yourself extending the Answer interface
  • Try the Mockito on Android, thanks to the Google guys working on dexmaker (more on that later)

Remember

  • Do not mock types you don't own
  • Don't mock value objects
  • Don't mock everything
  • Show some love with your tests

Click here for more documentation and examples. Remember all documentation lives in javadocs so you don’t need to visit that page too often. You can grab the RefCard? here.

If you have any suggestions, find documentation unclear or you found a bug, write to our mailing list. You can report bugs here.

Who is your bartender?

Mockito is served to you by Szczepan Faber and friends. First people who tried Mockito were developers of the Guardian project in London in early 2008. Here is how Szczepan explained why we need another mocking framework?

Firstly, hats down before EasyMock folks for their ideas on beautiful and refactorable mocking syntax. First hacks on Mockito were done on top of the EasyMock? code.

Here are just some of my friends who contributed ideas to Mockito (apologize if I missed somebody): Igor Czechowski, Patric Fornasier, Jim Barritt, Felix Leipold, Liz Keogh, Bartosz Bańkowski.

Now some other people joined the gang : Brice Dutheil and David Wallace.

Special thanks to Erik Ramfelt, and Steve Christou for putting Mockito on his Jenkins server (continuous integration).

Thanks to Karol Poźniak for the logo :)

Finally, thanks to Erik Brakkee who helps us getting jars to maven central

links wrap-up

Wiki

Blogs

Other project links

Groups