Open Source Mocking Frameworks in Java

EasyMock

EasyMock provides Mock Objects for interfaces in JUnit tests by generating them on the fly using Java’s proxy mechanism. Due to EasyMock’s unique style of recording expectations, most refactorings will not affect the Mock Objects. So EasyMock is a perfect fit for Test-Driven Development.

Go to EasyMock

Mockito

Mockito is a mocking framework that tastes really well. It lets you write beautiful tests with clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.

Mockito has very slim API, almost no time is needed to start mocking. There is only one kind of mock, there is only one way of creating mocks. Just remember that stubbing goes before execution, verifications of interactions go afterwards. You’ll soon notice how natural is that kind of mocking when TDD-ing java code.

Mockito has similar syntax to EasyMock, therefore you can refactor safely. Mockito doesn’t understand the notion of ‘expectation’. There is only stubbing and verifications.

Some features:

  • Mocks concrete classes as well as interfaces
  • Little annotation syntax sugar - @Mock
  • Verification errors are clean – click on stack trace to see failed verification in test; click on exception’s cause to navigate to actual interaction in code. Stack trace is always clean.
  • Allows flexible verification in order (e.g: verify in order what you want, not every single interaction)
  • Supports exact-number-of-times and at-least-once verification
  • Flexible verification or stubbing using argument matchers (anyObject(), anyString() or refEq() for reflection-based equality matching)
  • Allows creating custom argument matchers or using existing hamcrest matchers

Go to Mockito

Mockrunner

Mockrunner is a lightweight framework for unit testing applications in the J2EE environment. It supports Struts actions and forms, servlets, filters and tag classes. Furthermore it includes a JDBC and a JMS test framework. The JDBC test framework can be used standalone or in conjunction with MockEJB to test EJB based applications.

Go to Mockrunner

MockEJB

MockEJB is a lightweight framework for running EJBs. MockEJB implements javax.ejb APIs and creates Home and EJBObject implementation classes for your EJBs. Internally, MockEJB relies on dynamic proxies and interceptors. MockEJB has two primary usages:

  • It allows for running EJBs outside of the container for unit testing.You can run EJBs directly from your favorite IDE with the minimal setup effort.
  • It allows for deploying mock EJBs (i.e., mock EJB implementation classes) into the container. Mock EJBs provide very effective way of isolating EJBs under test from the rest of the application. Mock EJBs return predefined test data and can override the actual “non-mock” deployed EJBs. Once again, the purpose of MockEJB is twofold and it works inside and outside of the container. So you can benefit from it even if you continue running all your test classes inside the container using Cactus.

Additionally, MockEJB comes with the “mock” implementation of non-EJB APIs. Currently it provides in-memory JNDI and JMS implementations which can be used independently from MockEJB’s EJB support.

MockEJB is not a full-blown EJB container. It does not fully implement J2EE/EJB specs, however it supports all vital APIs.

Go to MockEJB

JMock

JMock is a library that supports test-driven development1 of Java2 code with mock objects3.

Mock objects help you design and test the interactions between the objects in your programs.

The jMock library:

  • makes it quick and easy to define mock objects, so you don’t break the rhythm of programming.
  • lets you precisely specify the interactions between your objects, reducing the brittleness of your tests.
  • works well with the autocompletion and refactoring features of your IDE
  • plugs into your favourite test framework
  • is easy to extend.

Go to JMock

Mocquer

Mocquer is a similar mock tool as EasyMock. With the help of Dunamis Project, it extends the function of EasyMock to support mock object creation for both class and interface.

Go to Mocquer

RMock

RMock is a Java mock object framework to use with jUnit. RMock has support for a setup-modify-run-verify workflow when writing jUnit tests. It integrates better with IDE refactoring support and allows designing classes and interfaces in a true test-first fashion.

Features:

  • Enhances both interaction-based and state-based testing
  • Explicit error messages to shorten your feedback cycle
  • Test-double creation for interfaces and classes
  • Setup expectations on the actual method of the mocked object to support easy refactoring
  • Expectation modification regarding:
    • arguments
    • return value
    • exception throwing
    • perform any generic action
    • call multiplicity
  • An assertThat( actual, <constraint>) method that gives you more power than traditional jUnit’s assertXxx(...), and more information when the assertion fails
  • A describable Expression/Constraint model supporting advanced asserts and error messages for expectations, objects and arrays.
  • A well defined workflow and clear test states
  • DynamicSuites that are flexibly configurable to include or exclude test classes depending on their runtime properties (no more include=“**/Test*”)
  • Nestable ordered and unordered sections for grouping and ordering expectations with clear error messages
  • Generate expectations for multiple methods with forEach(..) - if you, on a mock, want to setup expectations on all methods matching e.g. a specific name pattern, e.g. make sure that all get/set-methods on a mock are called, the forEach(..) setup can be used.
  • Documentation plugin (TDDoc). Document your test cases in your test cases. The RMock documentation is produced in that way (see “Documentation” in the left menu)

Go to RMock

SevenMock

SevenMock is a light-weight Java dynamic mock objects framework. It is unusual in that it places responsibility for verifying operation parameters directly on the unit test code. This enables the test designer to write very clear, precisely targeted tests and makes test failures easier to diagnose.

Go to SevenMock

JMockit

JMockit is open-source software licensed under the MIT License. It is a collection of tools for use in developer testing, that is, tests written by developers using a testing framework such as JUnit or TestNG. The tools rely on the Java 5 SE instrumentation feature (the java.lang.instrument package), internally using the ASM library to modify bytecode at runtime.

Go to JMockit

PowerMock

PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with EasyMock will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock extends the EasyMock API with a small number of methods and annotations to enable the extra features. From version 1.1 PowerMock also has basic support for Mockito.

Go to PowerMock

MockLib

MockLib is a small mock library that is extremely easy to learn with plenty of examples. Only has 5 classes for the api.

Go to MockLib

MockCreator

Given the interface or class, it generates source of Mock object for it.

Go to MockCreator