In this Java tutorial, we will go through some popular and most used Java testing frameworks used for mocking the test dependencies and verifying the results.
Mockito is an open-source framework that allows us to easily create test doubles (mocks). Test Double is a generic term for any case where you replace a production object for testing purposes.
In mockito, we generally work with the following kinds of test doubles.
- Stubs – is an object that has predefined return values to method executions made during the test.
- Spies – are objects that are similar to stubs, but they additionally record how they were executed.
- Mocks – are objects that have return values to method executions made during the test and have recorded expectations of these executions. Mocks can throw an exception if they receive a call they don’t expect and are checked during verification to ensure they got all the calls they were expecting.
We can mock both interfaces and classes in the test class. Mockito also helps to produce minimum boilerplate code while using mockito annotations.
EasyMock framework creates the mock objects using the java.lang.reflect.Proxy object. When we create a mock object, during test execution, the proxy object takes the place of the real object. The proxy object gets its fields and methods from the interface or class we pass when creating the mock.
A typical test with EasyMock has four stages:
- Create Mocks – whose behavior we want to delegate to the proxy objects. Generally, we mock the classes that interact with external systems or classes that should not be part of the test code.
- Record – expectations from the mock objects. These expectations include simulating a method with certain arguments, 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 then the mock will return the recorded results in the previous step.
- Verify – that all expectations were executed as recorded and no unexpected call was performed on a mock.
The microservices architecture allows us to develop, test and deploy different components of an application independently. Though such a component can be developed independently, testing this in isolation can be challenging. For a true integration testing of a microservice, we must test its interaction with other APIs.
WireMock helps in integration testing when we need to mock external APIs for testing a particular API dependent on those external APIs to complete a transaction. WireMock is a popular HTTP mock server that helps in mocking APIs and stubbing HTTP responses.
The MockWebServer is a helpful library to mock dependent APIs on which the current component (under test) depends. Such mock APIs are extremely helpful in microservices architecture where we are developing multiple dependent services at the same time.
MockWebServer is somewhat similar to WireMock, and it can be used to test async HTTP calls made from the Spring WebClient.
JMockit is open-source software that contains support for mocking, faking, and integration testing, and a code coverage tool. It is used for mocking the external dependencies outside the test boundary, similar to Mockito.
The most important feature of JMockit is that it lets us mock anything, even the things that are hard to mock with other libraries such as private methods, constructors, static and final methods. It even allows mocking the member fields and initialization blocks as well.
Similar to EasyMock, JMockit also uses the Record-Replay-Verify model in a test but allows defining the expectations and verifications in a very elaborative and declarative manner.
PowerMock extends the existing mocking frameworks, such as EasyMock and Mockito, to add even more powerful features to them. PowerMock enables us to write good unit tests for even the most untestable code.
For example, most of the mocking frameworks in Java cannot mock static methods or final classes. But using PowerMock, we can mock almost any class.
PowerMock currently extends the EasyMock and Mockito mocking frameworks. Depending on which extension is preferred, the syntax to write any unit test differs slightly.
Happy Learning !!