Mockito framework site (original) (raw)

Why drink it?

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 about features & motivations.

Use your own judgement in choosing a testing framework. The Mockito team always respects your choice. Keep writing great tests every day!

How do I drink it?

Recommended way of getting Mockito is declaring a dependencyon “mockito-core” library using your favorite build system. With Gradle one can do:

repositories { mavenCentral() }
dependencies { testImplementation "org.mockito:mockito-core:3.+" }

Maven users can declare a dependency on mockito-core. Mockito publishes every change as a -SNAPSHOT version to a public Sonatype repository.

Users doing manual dependency management can download the jars directly from Maven Central.

Legacy builds with manual dependency management can use the 1.* “mockito-all” distribution. Said distribution has been discontinued in Mockito 2.*.

now you can verify interactions

import static org.mockito.Mockito.*;

// mock creation
List mockedList = mock(List.class);
// or even simpler with Mockito 4.10.0+
// List mockedList = mock();

// using mock object - it does not throw any "unexpected interaction" exception
mockedList.add("one");
mockedList.clear();

// selective, explicit, highly readable verification
verify(mockedList).add("one");
verify(mockedList).clear();

and stub method calls

// you can mock concrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
// or even simpler with Mockito 4.10.0+
// LinkedList mockedList = mock();

// stubbing appears 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 stubbed
System.out.println(mockedList.get(999));

More info

Main reference documentation features:

Remember

Click here for more documentation and examples. All documentation lives in javadocs so you don’t need to visit that page too often. There is also a RefCard.

If you have suggestions, find documentation unclear, or find a bug, write to our mailing list. You can report feature requests and bugs in GitHub.

Mockito is served to you by Szczepan 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 needed another mocking framework?

Hats down before EasyMock 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 by Szczepan Faber, Brice Dutheil, Rafael Winterhalter, Tim van der Lippe, Marcin Grzejszczak, Marcin Zajączkowski and a small army 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 and Shipkit are used to facilitate continuous delivery. Both tools are great. Thank you GitHub, thank you Shipkit!

Training by core engineers

Mockito core engineers, experts in the field of software quality can help you with:

Get in touch at info@mockito.org