User:Meschwei/JMockit

JMockit is a Java (programming language) testing toolkit released under an open source MIT License. JMockit designed to include additional important mocking and testing capabilities that were not present in other common mocking frameworks. JMockit is compatible with common Java test automation frameworks like JUnit and TestNG. To achieve additional functionality, JMockit uses features common to debugging Java code to modify code in a JVM that is currently executing. This allows JMockit to temporarily change values of variables, methods, and more when running a test.

History
JMockit was originally released on June 6th, 2009 as version 0.81 and has and extensive revision history that has been regularly updated since then. It was primarily hosted on http://jmockit.googlecode.com but was later moved to http://www.jmockit.org.

Features
The JMockit toolkit contains many features similar to its competitors such as Mockito, Unitils Mock, and PowerMock. However, it has three unique features as well as a different approach to mocking that allows it to address the following three issues that other tool kits cannot address:
 * 1) Each class that needs to be mocked must have a separate interface implemented or must not be final.
 * 2) Tests will not be able to pass mock implementation of dependencies unless the dependencies are provided via a configurable instance creation method, or be exposed for dependency injection.
 * 3) Only Non-final instance methods can be mocked, so classes that are being tested cannot call or instantiate any static or final methods of the class dependencies.

Therefore JMockit has the following capabilities:
 * Mocks static and final methods, constructors and future instances
 * Mocks final classes, enums, annotation and multiple interfaces
 * Mocks implementation classes given just the interface or base class
 * Expressive recording and verification API
 * Concise argument matches and argument capturing
 * Dependency injection on both unit and integration tests

JMockit also comes with a code coverage tool that contains a set of metrics that can cover purely quantitative data of the target code. This tool is useful for test driven development of code as well as writing tests for existing production code.

Comparison to Other Mocking Frameworks
JMockit is often compared to its contemporaries such as: Mockito, JMock, and EasyMock. It has been reported that JMockit is more powerful and feature rich than these other mocking frameworks. However, discussions have also suggested that JMockit is harder to use and has a steeper learning curve.

Examples
The following examples use JMockit with JUnit to illustrate some of JMockit's mocking capabilities.

Accessing Private Fields
Consider the following class that one may wish to test.

Using JMockit, one is able to mock the private field called from the toString method with a JUnit 4 test.

Accessing Enums
JMockit is a notable for being mocking framework which can mock Enums. The following shows an Enum that represents different cat breeds.

Using JMockit, it is possible to mock out various parts of the Enum.