Spring Boot tests with Testcontainers and PostgreSQL, MySQL or MariaDB

Testcontainers is a Java library that allows integrating Docker containers in JUnit tests with ease. In a Containerized World, there is little sense to complicate the tests configuration with embedded databases and services. Instead, use run your services in Docker and let the Testcontainers manage this for you.

In this blog post you will learn how to configure Testcontainers to run PostgreSQL, MySQL and MariaDB in Spring Boot 2 integration tests.

This blog post covers:

  • Testcontainers configuration (via JDBC URL Scheme) for Spring Boot 2 tests with PostgreSQL, MySQL and MariaDB
  • Testcontainers in @DataJpaTest

Table of Contents


In order to use Testcontainers add the following dependencies to the pom.xml (assuming Maven based project):



where the DATABASE is one of postgresql, mysql, mariadb.

Note: Testcontainers provides JUnit 5 (Jupiter) plugin, but in the scenario presented in this pluging won’t be needed.

Test datasource configuration

Steps to configure Testcontainers for Spring Boot tests:

  • Set the driver to org.testcontainers.jdbc.ContainerDatabaseDriver which is a Testcontainers JDBC proxy driver. This driver makes will be responsible for starting the required Docker container when the datasource is initialized.
  • Set the dialect explicitly to implementation of the dialect for your database otherwise you get the exception while starting the application. This step is required when you use JPA in your application (via Spring Data JPA)
  • Set the JDBC URL to jdbc:tc:<database-image>:<version>:/// so that Testcontainers knows which database image to use.

PostgreSQL configuration:

The complete configuration:


MySQL configuration:


MariaDB configuration:


See more on database configuration in the official documentation here: https://www.testcontainers.org/modules/databases/

Initializing test database with Testcontainers

You may initialize the database with the script loaded by Testcontainers. The file can be loaded either directly from the classpath or from any location. The only thing to do is to change the JDBC URL:





In order to use TC in @DataJpaTest you need to make sure that the application defined (auto-configured) datasource is used. You can do it easily by annotating your test with @AutoConfigureTestDatabase as shown below:

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class OwnerRepositoryTests {

    private OwnerRepository ownerRepository;

    void findAllReturnsJohnDoe() { // as defined in tc-initscript.sql
        var owners = ownerRepository.findAll();


@SpringBootTest will use application defined datasource, so no additional changes are needed.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OwnerResourceTests {

    WebApplicationContext wac;

    void findAllReturnsJohnDoe() throws Exception {
                        "_embedded.owners.firstName", containsInAnyOrder("John"),
                        "_embedded.owners.lastName", containsInAnyOrder("Doe")


You have just learned the easiest way to configure PostgreSQL, MySQL and MariaDB with Testcontainers in Spring Boot integration tests. This solution is well suited for rather simple setup. If you need more control over the Docker images, please consult official Testcontainers documentation.

Source code

The source code for this article can be found on Github: https://github.com/kolorobot/spring-boot-tc

See also

Popular posts from this blog

Parameterized tests in JavaScript with Jest