simplier & better mocking
See the release notes page and latest documentation.
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).
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).
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.
Recommended way of getting Mockito is declaring a dependency on "mockito-core" library (not "mockito-all"!) using your favorite build system. My favorite build system happens to be Gradle and so I do:
repositories {
jcenter()
}
dependencies {
testCompile "org.mockito:mockito-core:1.+"
}
For legacy builds with manual dependency management it might be useful to use mockito-all package that can be downloaded from Mockito's Bintray repository or Bintray's jcenter.
For more information please refer to the wiki page.
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();
// 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));
Main reference documentation features:
mock()
/@Mock
: Create a mock (specify how it should behave viaAnswer
/ReturnValue
/MockSettings
)spy()
/@Spy
: Calls a real object but records what has been called,@InjectMocks
: Used as a kind of simple automatic dependency injection of fields created using the@Spy
or@Mock
annotation.verify()
to check methods were called with given arguments; can match any expression via theany()
, or capture what arguments where called using@Captor
instead.- Try BDD syntax with BDDMockito
- Try the Mockito on Android, thanks to the Google guys working on dexmaker (more on that later)
- 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.
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