Mockito

from Wikipedia, the free encyclopedia
mockito

Mockito logo
Basic data

Maintainer Szczepan Faber
Current  version 3.0.0
(July 8, 2019)
operating system Cross-platform
programming language Java
category Test - Framework
License WITH
mockito.org

Mockito is a free ( MIT license ) program library for creating mock objects for unit tests of Java programs.

Functionality

In the unit test, individual test objects (mostly classes or methods ) should be tested in isolation from their environment. In order to achieve a completely isolated test, the interfaces through which the object to be tested accesses its environment must be replaced by mock objects. The mock objects act as placeholders for the real objects. Mockito helps the developer of the unit tests to generate these mock objects and their behavior and, if necessary, to check how they were called by the code to be tested. Similar to other mocking frameworks, the mock objects are generated dynamically at runtime. There is therefore no need to write classes by hand or to keep their source code synchronized with that of the real classes. Dynamic mock objects are therefore safer than refactoring . Using Mockito, classes and interfaces can be mocked equally.

Mockito differs from other mocking frameworks in that with Mockito the developers can verify the behavior of the system under test without making any assumptions in advance. This reduces the often criticized close coupling of unit tests to the tested code.

history

The mockito project was launched in 2007 by the programmer Szczepan Faber, who was dissatisfied with the complexity of existing mocking frameworks. He started by expanding the syntax and functionality of EasyMock - a similar mocking framework for Java - but ended up rewriting most of the code for Mockito. The first version of Mockito was used in early 2008 for a project at The Guardian in London.

use

The following steps are carried out to use Mockito:

  • Create a mock object of the class or interface that is to be simulated:
List mockedList = mock(List.class);
  • Use mock object:
mockedList.add("one");
mockedList.clear();
  • Verify that the mock object was used as intended:
verify(mockedList).add("one");
verify(mockedList).clear();

See also

Web links

Individual evidence

  1. Features and Motivations. Retrieved May 19, 2011 .
  2. Martin Fowler: Mocks Aren't Stubs. 2007, accessed on May 19, 2011 .
  3. ^ Szczepan Faber: Mockito. January 14, 2008, accessed May 19, 2011 .
  4. mockito. simpler & better mocking. Retrieved May 19, 2011 .