Testing Applications with JUnit5 and JMock. Part 1
Testing Applications with JUnit5 and JMock. Part 1
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 take a closer look on three of the most widely-used mock frameworks: EasyMock, JMock and Mockito. We continue the series with JMock.
21 Sep 2020
1989
Other articles
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Last Modified technique?
How to implement Slowly Changing Dimensions(SCD) Type 2 in Spark?
How to incrementally migrate the data from RDBMS to Hadoop using Sqoop Incremental Append technique?
Why MongoDB don't fetch all the matching documents for the query fired
How to solve the issue of full disk utilization in HDFS Namenode
Can We Use HDFS as Back-up Storage?
How to do Indexing in MongoDB with Elastic Search? Part 1
How to do Indexing in MongoDB with Elastic Search? Part 2
How to store data on browser using NoSQL IndexedDB?
How to Apply MBTI in HR: Motivation for every day. Groups of People & their Motivations
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 take a closer look on three of the most widely-used mock frameworks: EasyMock, JMock and Mockito. We continue the series with JMock.
This far we saw how to implement our own mock-objects and how to use the EasyMock framework. In this section we introduce the JMock framework (http://jmock.org/), to get the full view on the different mocking techniques. We’ll follow the same scenario t: testing money transfer with the help of a mock AccountManager, this time using JMock.
In order to work with JMock, we need to add to the pom.xml file the dependencies from listing 1:
Listing 2 presents a very simple Account object with two properties: an account ID and a balance.
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 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.
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.
Catalin Tudose
Java and Web Technologies Expert
1. Using JMock
This far we saw how to implement our own mock-objects and how to use the EasyMock framework. In this section we introduce the JMock framework (http://jmock.org/), to get the full view on the different mocking techniques. We’ll follow the same scenario t: testing money transfer with the help of a mock AccountManager, this time using JMock.
In order to work with JMock, we need to add to the pom.xml file the dependencies from listing 1:
Listing 2 presents a very simple Account object with two properties: an account ID and a balance.
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 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.
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.
Interested in JUnit? Check out our trainings.
Catalin Tudose
Java and Web Technologies Expert