Over a million developers have joined DZone.

DAO Integration Test With TestContainers, Spring Boot, Liquibase, and PostgresSQL

DZone's Guide to

DAO Integration Test With TestContainers, Spring Boot, Liquibase, and PostgresSQL

In this article, we will cover how to do a DAO integration test when you have a spring boot application with PostgreSQL DB and Liquibase for schema versioning.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

Image title

In this article, we will cover how to do a DAO integration test when you have a spring boot application with PostgreSQL DB and Liquibase for schema versioning. Last time, we explained how to do the same without Liquibase using embedded PostgreSQL, but this time, we will do the same but use Docker with test containers, which will apply your Liquibase changes and make sure your DAO integration test has the same environment as the production database environment if you are using Docker for your database in production.

The whole code example is on GitHub.

Maven Dependencies

Beside spring boot starter dependencies, we will need to add Liquibase and test containers dependencies. The whole maven pom file can be viewed in the GitHub code.


Your Liquibase database simple master configuration will be in your resources source set, which will create a simple customer table with the file name changelog-master.xml.

Spring Boot DAO Integration Test Configuration

We will highlight the important sections. The whole Java file configuration can be viewed here: DbConfig.java

Data source configuration for test:

public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
    // here we reference the static test container variable in our test case to get the used the connection details
ds.setUrl(format("jdbc:postgresql://%s:%s/%s", postgreSQLContainer.getContainerIpAddress(),
PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
return ds;

Spring Liquibase configuration for test:

public SpringLiquibase springLiquibase(DataSource dataSource) throws SQLException {
    // here we create the schema first if not yet created before 
SpringLiquibase liquibase = new SpringLiquibase();
    // we want to drop the datasbe if it was created before to have immutable version
    //you set the schema name which will be used into ur integration test
    // the classpath reference for your liquibase changlog 
return liquibase;

Do not forget to disable the hibernate DDL generation in hibernate properties, as you can see in the Dbconfig:

ps.put("hibernate.hbm2ddl.auto", "none");

We use Liquibase for our schema definition and versioning.

Now how can we trigger the DAO integration test and use that configuration in practice?

@ContextConfiguration(classes = {DbConfig.class})
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:dao/TestData.sql")
public class PostgresEmbeddedDaoTestingApplicationTests {
  /* Here we have a static postgreSQL test container Class rule to make the instance used 
  for all test methods in the same test class , and we use @Transactional to avoid any dirty data changes between
  different test methods , where we pass our test database configuration ,
  ideally that should be loaded from external config file */
public static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer(

private CustomerRepository customerRepository;

public void contextLoads() {

.id(new Random().nextInt())

Assert.assertTrue(customerRepository.findCustomerByName("TestName") != null);

When you run the test from your IDEA, you should see that the Spring Boot application is started properly, and the docker image of the PostgreSQL is started with Liquibase changes applied so you can trigger your DAO integration test in a production-like situation.

The PostgreSQL docker image starts off-course. You need Docker kernel installed on your machine to be able to test the same:

The docker image start for PostgreSQL test container

Liquibase changes are being applied, as you can see in the console logs:

Liquibase changes being applied for the customer table


  • TestContainers: https://www.testcontainers.org/
  • Liquibase: https://www.Liquibase.org/
  • Spring Boot: https://spring.io/projects/spring-boot

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

spring-boot ,testcontainers ,postgresql 10.x ,liquibase ,java ,docker

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}