There are a lot of good projects already written to help us facilitate the usage of mock objects in our Java projects. In this series of articles, we’ll take a closer look on three of the most widely-used mock frameworks: EasyMock, JMock and Mockito. We'll start the series with EasyMock.
1. Using EasyMock
is an open-source framework which provides useful classes for mocking objects.
In order to work with it, we need to add to the pom.xml file the dependencies from listing 1:
Listing 1 EasyMock Maven dependencies
Listing 2 presents a very simple Account object with two properties: an account ID and a balance.
Listing 2 The Account class
Listing 3 shows the AccountManager
interface that manages the life cycle and persistence of Account
objects (limited to finding accounts by ID and updating accounts):
Listing 3 The AccountManager interface
Listing 4 shows the shows the transfer
method designed for transferring money between two accounts. It uses the previously defined AccountManager
interface to find the debit and credit accounts by ID and to update them.
Listing 4 The AccountService class
We want to be able to unit test the AccountService.transfer
behavior. For that purpose, until the implementation of the AccountManager
interface is ready, we will use a mock implementation of the AccountManager
interface because the transfer method is using this interface, and we need to test it in isolation.
Trying to introduce EasyMock, we create the TestAccountService
test using EasyMock, as in listing 5.
Listing 5 The TestAccountService class
What we do is:
- We start the listing defining the imports from the EasyMock library that we need (1). We see that we heavily rely in static imports.
In (2) we declare the object that we’d like to mock. Notice that our AccountManager is an interface; the reason behind this is simple – the core EasyMock framework can only mock interface objects.
In (3) we call the createMock method to create a mock of the class that we want.
In (4) we create two account objects which we’ll use in our tests. After that, we start declaring our expectations.
With EasyMock we declare the expectations in two ways. When the method return type is voided, we call it on the mock-object (as in (5)), or when the method returns any kind of object, then we need to use the expect-andReturn methods from the EasyMock API (6).
Once we finish defining the expectations, we need to call the replay method to announce it (7).
In (8) we call the transfer method to transfer some money between the two accounts, and in (9) we assert the expected result.
The @AfterEach method which gets executed after every @Test method holds the verification of the expectations. With EasyMock we can call the verify method with any mock object (10), to verify that the method-call expectations we declare are triggered. Including the verification into the @AfterEach method allows us to easily introduce new tests, and we’ll rely on the execution of the verify method from here.
Interested in JUnit? Check out our trainings.
Java and Web Technologies Expert