Quarkus integration tests: MySQL and Testcontainers

Quarkus integration tests: MySQL and Testcontainers

·

2 min read

Quarkus is an open source Kubernetes-native Java framework that enables smaller, faster Java applications. It is great for building microservices, but not limited to just that.

Testcontainers is a Java library that allows the integration of Docker containers into tests. With it you can start databases (like MySQL, MongoDb, etc.), identity management systems (like Keycloak), message queues instances or anything that can be run as a Docker container and create integration tests without a dependency on a local installation.

This blog post explains how to configure Testcontainers to manage MySQL instance for Quarkus integration tests in application properties.

Dependencies

Add the following testcontainers dependencies to the pom.xml:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>1.15.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>mysql</artifactId>
    <version>1.15.3</version>
    <scope>test</scope>
</dependency>

Note: When this blog post was written Quarkus 2.2.3.Final (and earlier) was forcing docker-java dependency version to 3.2.8 which is not compatible with testcontainers version 1.16.0.

Datasource configuration

The default datasource configuration in Quarkus application (will be used in all active profiles if not overwritten):

# Datasource configuration
quarkus.datasource.db-kind=mysql
quarkus.datasource.jdbc.driver=com.mysql.cj.jdbc.Driver
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/${db.name}
quarkus.datasource.username=${db.username}
quarkus.datasource.password=${db.password}

Note: Quarkus built-in profiles are dev, prod and test. To overwrite default value you just need to add prefix like: %test.quarkus.datasource.username=test.

To use Testcontainers in a test profile you need the following configuration:

  • JDBC driver: org.testcontainers.jdbc.ContainerDatabaseDriver
  • Hibernate dialect: org.hibernate.dialect.MySQL5Dialect
  • JDBC URL: jdbc:tc:mysql:latest:///<db_name>

Test datasource configuration in Quarkus application using MySQL testcontainers:

# Datasource configuration - test profile
%test.quarkus.datasource.jdbc.driver=org.testcontainers.jdbc.ContainerDatabaseDriver

%test.quarkus.hibernate-orm.dialect=org.hibernate.dialect.MySQL5Dialect

%test.quarkus.datasource.jdbc.url=jdbc:tc:mysql:latest:///${db.name}

Other Quarkus built-in database kinds + Hibernate dialect for other popular databases:

  • DB2: db2 / org.hibernate.dialect.DB2Dialect
  • Derby: derby / org.hibernate.dialect.DerbyTenSevenDialect
  • H2: h2 / org.hibernate.dialect.H2Dialect
  • MariaDB: mariadb / org.hibernate.dialect.MariaDB102Dialect for MariaDB server 10.2
  • Microsoft SQL Server: mssql / org.hibernate.dialect.SQLServerDialect
  • MySQL: mysql /org.hibernate.dialect.MySQLDialect
  • Oracle: oracle /org.hibernate.dialect.OracleDialect
  • PostgreSQL: postgresql, pgsql or pg /org.hibernate.dialect.PostgreSQLDialect

Note: To load init script by Testcontainers you can add a parameter to the datasource url ?TC_INITSCRIPT=file:src/main/resources/db/init.sql

Run tests

For testing a setup run maven command: mvn clean test. In the logs you will see that a MySQL container was created and started.

Screenshot 2021-09-27 at 21.22.18.png

Source code

Can be found on Github: github.com/gwenu/blog-howto/tree/master/how..