Please see therelease notes page.
Updates are announced via ourmailing list.
Mockito downloads and instructions for setting up Maven, Gradle and other build systems are available from theCentral Repository.
The documentation for all versions is available onjavadoc.io(the site is updated within 24 hours of the latest release).
Still on Mockito 1.x? Seewhat’s new in Mockito 2!Mockito 3.x requires Java 8, but otherwise doesn’t introduce any breaking changes compared to the 2.x series.
Fancy getting world-wide visibility and building up an eternal fame of an OSS contributor?
Mockito is a mocking framework that tastes really good.It lets you write beautiful tests with a clean & simple API.Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.Read more aboutfeatures & motivations.
Massive StackOverflow community voted Mockito the best mocking framework for java.Even though StackOverflow shuns questions that likely raise emotional debates the fact isMockito has the most votes.
Top 10 Java library across all libraries, not only the testing tools.In late 2013 there was ananalysismade of 30.000 GitHub projects.Although Mockito reached number 9 in the main report, mockito-core and mockito-all are the same tooland therefore the factual position of Mockito is number 4, surpassing famous tools like Guava or Spring.Treat this study as an indicator of a big impact that Mockito makes every day on unit tests written in Java.
Dan North, the originator ofBehavior-Driven Development wrote this back in 2008:
“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”
Given the current popularity of Mockito, Dan was spot on with his prediction.
Use your own judgement in choosing a testing framework.The Mockito team always respects your choice. Keep writing great tests every day!
Recommended way of getting Mockito isdeclaring a dependencyon “mockito-core” library using your favorite build system.WithGradle one can do:
repositories{mavenCentral()}dependencies{testImplementation"org.mockito:mockito-core:3.+"}
Maven users can declare adependency on mockito-core.Mockito publishes every change as a-SNAPSHOT
version to a publicSonatype repository.
Users doing manual dependency management can download the jars directly fromMaven Central.
Legacy builds with manual dependency management can use the1.* “mockito-all” distribution. Said distribution has been discontinued in Mockito 2.*.
importstaticorg.mockito.Mockito.*;// mock creationListmockedList=mock(List.class);// or even simpler with Mockito 4.10.0+// List mockedList = mock();// using mock object - it does not throw any "unexpected interaction" exceptionmockedList.add("one");mockedList.clear();// selective, explicit, highly readable verificationverify(mockedList).add("one");verify(mockedList).clear();
// you can mock concrete classes, not only interfacesLinkedListmockedList=mock(LinkedList.class);// or even simpler with Mockito 4.10.0+// LinkedList mockedList = mock();// stubbing appears before the actual executionwhen(mockedList.get(0)).thenReturn("first");// the following prints "first"System.out.println(mockedList.get(0));// the following prints "null" because get(999) was not stubbedSystem.out.println(mockedList.get(999));
Main reference documentation features:
mock()
/@Mock
: create mockAnswer
/MockSettings
when()
/given()
to specify how a mock should behaveAnswer
interfacespy()
/@Spy
:partial mocking, real methods are invoked but still can be verified and stubbed@InjectMocks
: automatically inject mocks/spies fields annotated with@Spy
or@Mock
verify()
: to check methods were called with given argumentsClickhere for more documentation and examples.All documentation lives in javadocs so you don’t need to visit that page too often.There is also aRefCard.
If you have suggestions, find documentation unclear, or find a bug, write to ourmailing list.You can report feature requests and bugs inGitHub.
Mockito is served to you bySzczepan Faber and friends.First engineers who were using Mockito in production were developers of theGuardian project in London in early 2008.Szczepan was lucky to be a part of the ThoughtWorks team assigned to the challenging and exciting Guardian project.Here is how he explained why we neededanother mocking framework?
Hats down beforeEasyMock folks for their ideas on beautiful and refactoring-friendly mocking API.First hacks on Mockito were done on top of the EasyMock code.
Currently Mockito is maintained bySzczepan Faber,Brice Dutheil,Rafael Winterhalter,Tim van der Lippe, Marcin Grzejszczak, Marcin Zajączkowski anda smallarmy of contributors.Friends who contributed to Mockito (apologize if I missed somebody):Pascal Schumacher,Christian Schwartz,Igor Czechowski,Patric Fornasier,Jim Barritt,Felix Leipold,Liz Keogh,Dan North,Bartosz Bańkowski,David Wallace.
GitHub actions andShipkit are used to facilitate continuous delivery.Both tools are great. Thank you GitHub, thank you Shipkit!
Thanks to commercial software that help us build mockito
Mockito core engineers, experts in the field of software quality can help you with:
Get in touch atinfo@mockito.org
Mockito isopen source—Want tocontribute?—Issuetracker—Mailinglist—Trainingoffering