simplier & better mocking
Status update
18/03/2014 Mockito should work fine with JDK8 if you stay away from defender methods. Lambda usage may work just as good for Answer
s. We're unsure about every JDK8 features at the moment, like serializing a mock that uses a lambda. Error report and pull request are welcome though (contributing guide).
Current release
06/10/2012 Mockito 1.9.5 released! See the release notes. Should appear in maven central shortly.
Moving to github
We are currently moving a few stuff from Google Code to Github.
For now only the code repository is moved, other stuff will follow like the wiki pages. Documentation may or may not follow. But discussion group, issue tracker will stay at google (unfortunatley GH PR counters incremented before we had the chance to move our issues).
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
Over 15000 downloads of 1.9.0 version ('12), excluding maven/Gradle users. For latest figures see the downloads.
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 concrete 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 then David Wallace.
Special thanks to Erik Ramfelt, and Steve Christou for putting Mockito on there Jenkins server (continuous integration) for a few years. Now Travis and Coveralls took over.
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
- FAQ
- How to contribute
- Mockito for python
- Mockito VS EasyMock
- Related projects
- Release notes
- More wiki pages
- Old wiki pages on google code