When comparing JMockit vs Mockito, the Slant community recommends JMockit for most people. In the question“What are the best mock frameworks for Java?” JMockit is ranked 1st while Mockito is ranked 2nd. The most important reason people chose JMockit is:
The official tutorial contains thorough explanations and discussions of all methods, annotations, fields etc.., with lots of easy to understand examples. The API documentation is very complete as well and it provides detailed explanation for all elements of the mocking API.
Ranked in these QuestionsQuestion Ranking
Pro Great resources
The official tutorial contains thorough explanations and discussions of all methods, annotations, fields etc.., with lots of easy to understand examples.
The API documentation is very complete as well and it provides detailed explanation for all elements of the mocking API.
Pro Can mock everything
JMockit has the ability to mock final classes and final, private and static methods whereas other mocking frameworks only work on a few of these.
Pro Instance capture ability
Being able to capture instances and mock implementations as the test executes without having any knowledge of the actual implementation classes allows JMockit to verify these special objects through regular assertions with custom validation methods for each matching object.
Pro PowerMock extensions solve some difficult edge cases
PowerMock extensions to Mockito allow for testing static methods, etc.
Pro Verification and exceptions are clean and flexible
Users can easily track failed verifications in tests and these exceptions lead the user to the actual point of interaction in the code. The stack trace will always be clean and verification can be flexible in the order it is presented ensuring the most important verifications can be shown first.
Pro Simple, clean API
Mockito focuses on having a very simple and clean API to allow ease of use and a simple way to get started. It allows users to use the same techniques when mocking classes or interfaces as there is only one kind of mock and one way of creating mocks.
Pro Ease of use
Con Learning curve is a bit steep
Since JMockit has a much larger scope that other mocking frameworks and as a result, the learning curve will be a bit steep.
Con No static and final method support
Although Mockito is a mature framework it lacks the ability to mock static or private methods. This is because Mockito prefers object orientation and dependency injection over static, procedural code that is hard to understand & change.