Top Mock Testing Frameworks in Java

Learn some popular and the most used Java unit testing frameworks used for mocking the test dependencies and verifying the test results.

Mocking frameworks replace the dependencies of the code under test with test doubles and verify how the code used them. Mockito, EasyMock, JMockit and PowerMock mock Java classes and interfaces, whereas WireMock and MockWebServer mock the HTTP APIs that a component depends on. We use them in unit and integration tests, so a test does not need the real database, service or API. The sections below describe Mockito, EasyMock, WireMock, MockWebServer, JMockit and PowerMock in that order.

1. Mockito

Mockito is an open-source framework that creates test doubles (mocks). A test double is any object that replaces a production object in a test.

In Mockito, we work with the following kinds of test doubles.

  • A stub is an object that has predefined return values for the method calls made during the test.
  • A spy is similar to a stub, but it also records how the test called it.
  • A mock has predefined return values for the method calls made during the test and also has recorded expectations of these calls. A mock can throw an exception if it receives a call it does not expect, and the verification step checks that the mock got all the expected calls.

Mockito creates mocks for both interfaces and classes. Its annotations reduce the boilerplate code in the test class.

2. EasyMock

EasyMock creates mock objects with the class java.lang.reflect.Proxy. When the test runs, the proxy object takes the place of the real object, and it gets its fields and methods from the interface or class that we pass when we create the mock.

A typical test with EasyMock has the following four stages.

  • Create mocks for the classes whose behavior we delegate to the proxy objects. We mock the classes that interact with external systems or that should not be part of the test code.
  • Record the expectations of the mock objects. These expectations include the method and arguments to simulate, the return value of the invoked method and the number of times the method should be invoked.
  • Replay makes the mock object available. In replay mode, when the test invokes a recorded method, the mock returns the result recorded in the previous stage.
  • Verify that all the expectations were executed as recorded and that no unexpected call was made on a mock.

3. WireMock

In a microservices architecture, we develop, test and deploy the components of an application independently. Testing a component in isolation can be challenging, because a true integration test of a microservice must test its interaction with other APIs.

WireMock is a popular HTTP mock server that mocks APIs and stubs HTTP responses. We use it in integration tests when the API under test depends on external APIs to complete a transaction.

4. MockWebServer

MockWebServer is a library that mocks the APIs on which the component under test depends. Mock APIs are useful in a microservices architecture, where several dependent services are developed at the same time.

MockWebServer is similar to WireMock. We can use it to test asynchronous HTTP calls made from the Spring WebClient.

5. JMockit

JMockit is open-source software that supports mocking, faking and integration testing, and includes a code coverage tool. Similar to Mockito, it mocks the external dependencies outside the test boundary.

JMockit can mock private methods, constructors, static methods and final methods, which are hard to mock with other libraries. It can also mock member fields and initialization blocks.

Similar to EasyMock, JMockit uses the Record-Replay-Verify model in a test, and it defines the expectations and verifications in a declarative way.

6. PowerMock

PowerMock extends the existing mocking frameworks, such as EasyMock and Mockito, with features for code that is hard to test.

Most mocking frameworks in Java cannot mock static methods or final classes, whereas PowerMock can mock almost any class.

PowerMock currently extends EasyMock and Mockito, and the syntax of a unit test differs slightly depending on the extension we choose.

Happy Learning !!

Sourcecode on Github

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.