Mockito

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

Functionality

In the unit test individual test objects (mostly classes or methods ) to be tested isolated from their surroundings. To achieve a completely isolated test, the interfaces on the access object to be tested on its surroundings need to be replaced with mock objects. The mock objects act as placeholders for the real objects. Mockito helps the developers of the unit tests, these mock objects to generate, together with their behavior and possibly also to consider how they have been called by the code under test. The mock objects are generated dynamically as in other Mocking frameworks at runtime. There must therefore be no classes written by hand or whose source code will allow the real classes kept in sync. Dynamic mock objects are thus safer compared to refactoring. Using Mockito can be gemockt classes and interfaces alike.

Mockito is different from other Mocking frameworks in that with Mockito developers can verify the behavior of the system under test without hitting some assumptions in advance. Thus, the often criticized tight coupling of unit tests is reduced to the code under test.

History

Szczepan Faber started in 2007 Mockito the project because he was dissatisfied with the complexity of existing Mocking frameworks. Szczepan began with the extension of the syntax and functionality of EasyMock - a similar Mocking framework for Java - but ultimately wrote most of the code for Mockito to. The first version of Mockito has already been used in early 2008 for a project at The Guardian in London.

The current major version 1.9 brought the following improvements: Improved documentation, smarter annotation, less code needed to generate stubs.

Use

Use of Mockito following steps are performed:

  • Generate mock object of the class or interface that is to be simulated: List mockedList = mock ( List.class );
  • Mock object use: mockedList.add ("one "); mockedList.clear ();
  • Verify whether the mock object was used as required: verify ( mockedList ) add ( "one" ). ; verify ( mockedList ) clear ( ). ;
577268
de