Mockito

Last updated
Mockito
Developer(s) Szczepan Faber, Brice Dutheil, Rafael Winterhalter, Tim van der Lippe and others.
Stable release
5.18.0 / May 20, 2025;2 months ago (2025-05-20) [1]
Repository github.com/mockito/mockito
Written in Java
Type Testing
License MIT License [2]
Website site.mockito.org

Mockito is an open source testing framework for Java released under the MIT License. [3] [4] The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of test-driven development (TDD) or behavior-driven development (BDD).

Contents

The framework's name and logo are a play on mojitos, a type of drink.

Features

Mockito allows developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand. [5] One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test. [6] Mockito attempts to eliminate the expect-run-verify pattern [7] by removing the specification of expectations. Mockito also provides some annotations for reducing boilerplate code. [8]

Origins

Mockito began by expanding on the syntax and functionality of EasyMock. [9] [10]

Example

Consider this decoupled Hello world program; we may unit test some of its parts, using mock objects for other parts.

packageorg.examples;importjava.io.IOException;publicclassHelloApplication{publicstaticinterfaceGreeter{StringgetGreeting(Stringsubject);StringgetIntroduction(Stringactor);}publicstaticclassHelloGreeterimplementsGreeter{privateStringhello;privateStringsegmenter;publicHelloGreeter(Stringhello,Stringsegmenter){this.hello=hello;this.segmenter=segmenter;}publicStringgetGreeting(Stringsubject){returnhello+" "+subject;}publicStringgetIntroduction(Stringactor){returnactor+segmenter;}}publicstaticinterfaceHelloActable{voidsayHello(Stringactor,Stringsubject)throwsIOException;}publicstaticclassHelloActionimplementsHelloActable{privateGreeterhelloGreeter;privateAppendablehelloWriter;publicHelloAction(GreeterhelloGreeter,AppendablehelloWriter){super();this.helloGreeter=helloGreeter;this.helloWriter=helloWriter;}publicvoidsayHello(Stringactor,Stringsubject)throwsIOException{helloWriter.append(helloGreeter.getIntroduction(actor)).append(helloGreeter.getGreeting(subject));}}publicstaticvoidmain(String...args)throwsIOException{newHelloAction(newHelloGreeter("hello",": "),System.out).sayHello("application","world");}}

The result of HelloApplication launching will be the following:

application: hello world 

Unit test for HelloActable component may look like this:

packageorg.examples;import staticorg.mockito.Matchers.any;import staticorg.mockito.Matchers.eq;import staticorg.mockito.Mockito.mock;import staticorg.mockito.Mockito.times;import staticorg.mockito.Mockito.verify;import staticorg.mockito.Mockito.when;importorg.junit.Before;importorg.junit.Test;importorg.examples.HelloApplication.HelloActable;importorg.examples.HelloApplication.HelloAction;importorg.examples.HelloApplication.Greeter;publicclassHelloActionUnitTest{GreeterhelloGreeterMock;AppendablehelloWriterMock;HelloActablehelloAction;@BeforepublicvoidsetUp(){helloGreeterMock=mock(Greeter.class);helloWriterMock=mock(Appendable.class);helloAction=newHelloAction(helloGreeterMock,helloWriterMock);}@TestpublicvoidtestSayHello()throwsException{when(helloWriterMock.append(any(String.class))).thenReturn(helloWriterMock);when(helloGreeterMock.getIntroduction(eq("unitTest"))).thenReturn("unitTest : ");when(helloGreeterMock.getGreeting(eq("world"))).thenReturn("hi world");helloAction.sayHello("unitTest","world");verify(helloGreeterMock).getIntroduction(eq("unitTest"));verify(helloGreeterMock).getGreeting(eq("world"));verify(helloWriterMock,times(2)).append(any(String.class));verify(helloWriterMock,times(1)).append(eq("unitTest : "));verify(helloWriterMock,times(1)).append(eq("hi world"));}}

It uses mock objects for the Greeter and Appendable interfaces, and implicitly assumes the next use case:

unitTest : hi world 

Integration test code for testing HelloActable wired together with Greeter may look like the following:

packageorg.examples;import staticorg.mockito.Matchers.any;import staticorg.mockito.Matchers.eq;import staticorg.mockito.Mockito.mock;import staticorg.mockito.Mockito.times;import staticorg.mockito.Mockito.verify;import staticorg.mockito.Mockito.when;importorg.junit.Before;importorg.junit.Test;importorg.examples.HelloApplication.HelloActable;importorg.examples.HelloApplication.HelloAction;importorg.examples.HelloApplication.Greeter;importorg.examples.HelloApplication.HelloGreeter;publicclassHelloActionIntegrationTest{HelloActablehelloAction;GreeterhelloGreeter;AppendablehelloWriterMock;@BeforepublicvoidsetUp(){helloGreeter=newHelloGreeter("welcome"," says ");helloWriterMock=mock(Appendable.class);helloAction=newHelloAction(helloGreeter,helloWriterMock);}@TestpublicvoidtestSayHello()throwsException{when(helloWriterMock.append(any(String.class))).thenReturn(helloWriterMock);helloAction.sayHello("integrationTest","universe");verify(helloWriterMock,times(2)).append(any(String.class));verify(helloWriterMock,times(1)).append(eq("integrationTest says "));verify(helloWriterMock,times(1)).append(eq("welcome universe"));}}

It uses mock objects only in place of Appendable interfaces, uses the real implementations for other (HelloActable and Greeter) interfaces, and implicitly assumes the next use case:

integrationTest says welcome universe 

As can be seen from the import statements of HelloActionUnitTest and HelloActionIntegrationTest classes, it is necessary to put some Mockito jars and JUnit jars in your class path to be able to compile and run the test classes.

See also

References

  1. "Project Releases Overview on GitHub". GitHub . Retrieved 29 July 2025.
  2. "License · mockito/mockito Wiki · GitHub". GitHub . Retrieved 30 August 2019.
  3. "Mockito in six easy examples". 2009. Retrieved 2012-10-05.
  4. "What's the best mock framework for Java?" . Retrieved 2010-12-29.
  5. "Features and Motivations" . Retrieved 2010-12-29.
  6. Fowler, Martin (2007). "Mocks Aren't Stubs" . Retrieved 2010-12-29.
  7. Faber, Szczepan. "Death Wish" . Retrieved 2010-12-29.
  8. Kaczanowski, Tomek. "Mockito - Open Source Java Mocking Framework" . Retrieved 2013-09-17.
  9. Faber, Szczepan. "Mockito". Archived from the original on 2010-03-29. Retrieved 2010-12-29.
  10. "Mockito Home Page" . Retrieved 2010-12-29.