Testing the Hibernate layer with Docker

Kai Winter
4 min readJun 6, 2016

--

This article has been updated in March 2023 for Java 17, Hibernate 6, JUnit 5 and Testcontainers 1.17.6.

Did you ever feel the need to write unit tests for your persistence layer? We know logic doesn’t belong there, it should just do some CRUD operations and nothing more. But in reality some kind of logic can be found in the most persistence layers. Some use heavy native SQL, some others make use of the Criteria API which is test-worthy because of it’s complex syntax.

We are all writing unit tests for the service layer but the persistence layer in contrast is rarely tested as it is more complicated to test SQL or JPQL or any other QL’es.

An example

One example of logic which is better be tested is the following. This is a trivial example which may be far more complex in reality. I’ll use this to demonstrate how to test persistence related code.

/**
* Resets the login count for other users than root and admin.
*/
public void resetLoginCountForUsers() {
Query query = entityManager.createQuery(
"UPDATE User "
+ "SET loginCount=0 "
+ "WHERE username NOT IN ('root', 'admin')");
query.executeUpdate();
}

Previously in our toolbox

In the past there were several approaches to test the persistence layer:

  • In memory database like HSQL
    - Pro: clean database on every run
    - Con: different database than in production (Oracle, MySQL, …)
    - Con: doesn’t work when you need stored procedures
  • Local database on the dev machine
    - Pro: same database as production
    - Con: have to take care of deleting previous data
    - Con: possible side effects when database is used for development also
    - Con: have to synchronize settings on dev machines and CI system
  • Dummy database driver which returns pseudo data from CSV files
    - Pro: Lightweight
    - Con: hard to setup for a large domain model, model might be invalid

New kid on the block: TestContainers

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. [testcontainers.org]

This library starts a docker container which contains any database you like. Your tests gets a connection on that database and you can start inserting your test data per each single unit test.

  • Pro: absolutely reproducible test
  • Pro: same database as in production
  • Pro: configuration is shared above all machines via the docker image
  • Con: a bit slower because of database start (depending on used database)
  • Con: You need a Docker host against which the tests can run. This may either be centralized so all developers are using the same (which is no problem even when starting containers concurrently), or you have a Docker installation on your local machine.

This is how the test looks like. The data model is already inserted (by persistence.xml, see below), the test inserts it’s test data by DbUnit, calls the persistence layer method and makes the asserts. The transaction have to be managed by hand as we have no container which is doing this for us. To make this more convenient, the transaction management can be wrapped in a method and a lambda function, like: runWithTransaction(() -> userRepository.resetLoginCountForUsers());

@Test
void testResetLoginCountForUsers() {
DockerDatabaseTestUtil
.insertDbUnitTestdata(entityManager,
getClass().getResourceAsStream("/testdata.xml));

entityManager.getTransaction().begin();
userRepository.resetLoginCountForUsers();
entityManager.getTransaction().commit();
User rootUser = userRepository.findByUsername("root");
User adminUser = userRepository.findByUsername("admin");
User user = userRepository.findByUsername("user");
assertEquals(5, rootUser.getLoginCount());
assertEquals(3, adminUser.getLoginCount());
assertEquals(0, user.getLoginCount()); // PREVIOUSLY WAS 1
}

Using TestContainers

First the persistence.xml is prepared to use a special driver which comes with the jdbc-module of TestContainers.

<properties>
<property
name="jakarta.persistence.jdbc.driver"
value="org.testcontainers.jdbc.ContainerDatabaseDriver"/>
<property
name="jakarta.persistence.jdbc.url"
value="jdbc:tc:mysql:5.7://xxx/test?TC_INITSCRIPT=DDL.sql"/>
</properties>

The driver org.testcontainers.jdbc.ContainerDatabaseDriver is registered in the jdbc module. It evaluates the jdbc.url to start the right docker image, in this case mysql:5.7. The suffix ?TC_INITSCRIPT=DDL.sql initializes the database with the DB model. There are also other ways to initialize the database (see below).

The initialization is triggered by Hibernate with the creation of the EntityManager in the test:

EntityManager em =
Persistence.createEntityManagerFactory("TestUnit")
.createEntityManager();

This will make Hibernate parse the persistence.xml and looking up the jdbc driver which triggers TestContainers to start the MySQL docker container. (The database’s port is exposed on a random port.) The resulting EntityManager is pointing to the started and initialized docker container and can be used:

class UserTest {
EntityManager entityManager = Persistence
.createEntityManagerFactory("TestPU")
.createEntityManager();
@Test
void testSaveAndLoad() {
User user = new User();
user.setUsername("user 1");

entityManager.getTransaction().begin();
entityManager.persist(user);
entityManager.getTransaction().commit();
User userFromDb = entityManager.find(User.class,user.getId());
assertEquals(user.getUsername(), userFromDb.getUsername());
}
}

The complete test class is here: UserTest

The complete test from the beginning which calls the repository is here: UserRepositoryTest.

Different ways to insert the data model and test data

There are several ways to get your data model and test data into the database.

  • Like seen before the connection string in the persistence.xml: The property suffix ?TC_INITSCRIPT=DDL.sql initializes the database with the DB model from DDL.sql
  • Execute SQL File(s) from your test, see DockerDatabaseTestUtil.insertSqlFile()
  • Execute single SQL Queries from your test, see DockerDatabaseTestUtil.insertSqlString()
  • Use DbUnit to insert test data from XML files, see DockerDatabaseTestUtil.insertDbUnitTestdata()
  • The model can also be generated by Hibernate by setting the property in the persistence.xml hibernate.hbm2ddl.auto

My favorite is to insert the DDL via the persistence.xml and then let every test case insert it’s own data by DbUnit.

You can find the complete code examples on GitHub.

What’s next

Was this article useful for you? Please share your thoughts in the comments. Follow my account and you will be notified about my upcoming article about using TestContainers to deploy to a Wildfly server running in a docker container.

--

--