DAO integration test with TestContainers , Spring boot , Liquibase and PostgresSQL

We will cover here how to do DAO integration test when you have spring boot application with PostgreSQL DB and liquibase for schema versioning , last time we have explained how to do the same without liquibase using embedded PostgreSQL in this post but this time we will do the same but using docker with test containers which will apply your liquibase changes and making sure you DAO integration test has the same environment as production database environment if you are using docker for your data base 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 , whole maven pom file can be viewed in Github code

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.10.5</version>
</dependency>

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

Spring boot DAO integration test configuration :

We will highlight the important sections , the whole configuration java file can be viewed into the github code : DbConfig.java

Data source configuration for test:

@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.postgresql.Driver");
// 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.getMappedPort(
PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
ds.setUsername(postgreSQLContainer.getUsername());
ds.setPassword(postgreSQLContainer.getPassword());
ds.setSchema(postgreSQLContainer.getDatabaseName());
return ds;
}

Spring liquibase configuration for test :

@Bean
public SpringLiquibase springLiquibase(DataSource dataSource) throws SQLException {
// here we create the schema first if not yet created before
tryToCreateSchema(dataSource);
SpringLiquibase liquibase = new SpringLiquibase();
// we want to drop the datasbe if it was created before to have immutable version
liquibase.setDropFirst(true);
liquibase.setDataSource(dataSource);
//you set the schema name which will be used into ur integration test
liquibase.setDefaultSchema("test");
// the classpath reference for your liquibase changlog
liquibase.setChangeLog("classpath:/db/changelog/changelog-master.xml");
return liquibase;
}

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

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

As we use liquibase for our schema definition and versioning .

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DbConfig.class})
@ActiveProfiles("DaoTest")
@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 */
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer(
"postgres:10.3")
.withDatabaseName("test")
.withUsername("user")
.withPassword("pass").withStartupTimeout(Duration.ofSeconds(600));
@Autowired
private CustomerRepository customerRepository;
@Test
@Transactional
public void contextLoads() {
customerRepository.save(Customer.builder()
.id(new Random().nextInt())
.address("brussels")
.name("TestName")
.build());
Assert.assertTrue(customerRepository.findCustomerByName("TestName") != null);
}
}
view raw DAoTest.java hosted with ❤ by GitHub

Where when you run the test from your IDEA , you should see 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 a production like situation .

The PostgreSQL docker image starting , off-course you need docker kernel installed in our machine to be able to test the same :

the docker image start for postgreSQL test container

And liquibase changes being applied as you can see in the console logs :

liquibase changes being applied for customer table

References :

Leave a Reply