Puneet Varma (Editor)

Mockito

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

Mockito is an open source testing framework for Java released under the MIT License. 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

In software development there is an opportunity of ensuring that objects perform the behaviors that are expected of them. One approach is to create a test automation framework that actually exercises each of those behaviors and verifies that it performs as expected, even after it is changed. However, the requirement to create an entire testing framework is often an onerous task that requires as much effort as writing the original objects that were supposed to be tested. For that reason, developers have created mock testing frameworks. These effectively fake some external dependencies so that the object being tested has a consistent interaction with its outside dependencies. Mockito intends to streamline the delivery of these external dependencies that are not subjects of the test. A study performed in 2013 on 10,000 GitHub projects found that Mockito is the 9th most popular Java library.

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

Distinguishing features

Mockito distinguishes itself from other mocking frameworks by allowing developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand. One of the criticisms of mock objects is that there is a tight coupling of the test code to the system under test. Since Mockito attempts to eliminate the expect-run-verify pattern by removing the specification of expectations, the coupling is reduced or minimized. The result of this distinguishing feature is simpler test code that should be easier to read and modify. Mockito also provides some annotations useful for reducing boilerplate code.

Origins

Szczepan Faber started the Mockito project after finding existing mock object frameworks too complex and difficult to work with. Faber began by expanding on the syntax and functionality of Easy Mock, but eventually rewriting most of Mockito. Faber's goal was to create a new framework that was easier to work with at the Guardian project in London in early 2008.

Usage

Mockito has a growing user-base as well as finding use in other open source projects.

Example

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

The result of HelloApplication launching will be the following:

application: hello world

Unit test for HelloActable component may look like this:

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:

It uses mock objects only in place of Appendable interfaces, and 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.

References

Mockito Wikipedia