Mockito Explained

Mockito
Developer:Szczepan Faber, Brice Dutheil, Rafael Winterhalter, Tim van der Lippe and others.
Latest Release Version:5.4.0
Latest Release Date:[1]
Programming Language:Java
Genre:Testing
License:MIT License[2]

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).

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.

package org.examples;

import java.io.IOException;

public class HelloApplication

The result of HelloApplication launching will be the following:

application: hello world

Unit test for HelloActable component may look like this:

package org.examples;

import static org.mockito.Matchers.any;import static org.mockito.Matchers.eq;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;

import org.junit.Before;import org.junit.Test;

import org.examples.HelloApplication.HelloActable;import org.examples.HelloApplication.HelloAction;import org.examples.HelloApplication.Greeter;

public class HelloActionUnitTest

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:

package org.examples;

import static org.mockito.Matchers.any;import static org.mockito.Matchers.eq;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;

import org.junit.Before;import org.junit.Test;

import org.examples.HelloApplication.HelloActable;import org.examples.HelloApplication.HelloAction;import org.examples.HelloApplication.Greeter;import org.examples.HelloApplication.HelloGreeter;

public class HelloActionIntegrationTest

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

Notes and References

  1. Web site: Project Releases Overview on GitHub. . 29 April 2022.
  2. Web site: License · mockito/mockito Wiki · GitHub. . 30 August 2019.
  3. Web site: Mockito in six easy examples . 2009 . 2012-10-05 .
  4. Web site: What's the best mock framework for Java? . 2010-12-29 .
  5. Web site: Features and Motivations . 2010-12-29 .
  6. Web site: Mocks Aren't Stubs . Fowler . Martin . 2007 . 2010-12-29 .
  7. Web site: Death Wish . Faber . Szczepan . 2010-12-29 .
  8. Web site: Kaczanowski. Tomek . Mockito - Open Source Java Mocking Framework. 2013-09-17.
  9. Web site: Mockito . https://web.archive.org/web/20100329033254/http://monkeyisland.pl/2008/01/14/mockito/ . 2010-03-29 . Faber . Szczepan . 2010-12-29.
  10. Web site: Mockito Home Page . 2010-12-29 .