Spring Boot integration testing with Testcontainers starter project

In my previous blog post, I have demonstrated how to we can do hassle-free integration testing in Spring Boot using Testcontainers Java library. In this blog post I will show you, simple and non-intrusive way to use Testcontainers features in your project.

Testcontainers library has a starter project `testcontainers-spring-boot` from Playtika which further simplifies the usage. Using Testcontainers starter project you do not need to add Testcontainer related annotations in the test class. It requires only adding few dependencies in the pom file and configuration properties.

As I have already covered the basics of Testcontainers in my previous article, in this post I am going to show you only the modifications you need to do. If you are newly adding Testcontainers you just need to use the new modifications.

Change POM dependencies

Remove the following dependencies from pom.xml

<dependency> <groupId>org.testcontainers</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>postgresql</artifactId> <scope>test</scope> </dependency>
Code language: HTML, XML (xml)

And also remove the Testcontainer bom from pom.xml

<dependencyManagement> <dependencies> <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-bom</artifactId> <version>${testcontainers.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Code language: HTML, XML (xml)

Since we are using PostgreSQL in our project, add the following starter project dependencies to pom.xml

<dependency> <groupId>com.playtika.testcontainers</groupId> <artifactId>embedded-postgresql</artifactId> <version>2.0.7</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.0.2</version> <scope>test</scope> </dependency>
Code language: HTML, XML (xml)


testcontainers-spring-boot project migrated to Spring Boot 2.4 in 2.0.0 version. Please note, that in order to use this project with Spring Boot 2.4, you need to use spring-cloud-starter-bootstrap dependency. For earlier Spring Boot (prior to 2.4) users — you need to use spring-cloud-starter dependency instead.

Changes to Test class

Remove the Testcontainers related annotations and container classes.

Please see the sample code below.

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) //@Testcontainers public class BaseIT { @Autowired protected TestRestTemplate testRestTemplate ; // @Container // private static PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>(PostgreSQLContainer.IMAGE) // .withDatabaseName("testdb") // .withUsername("postgres") // .withPassword("secret") // .withInitScript("ddl.sql"); // @DynamicPropertySource // public static void properties(DynamicPropertyRegistry registry) { // // registry.add("spring.datasource.url", postgresDB::getJdbcUrl); // registry.add("spring.datasource.username", postgresDB::getUsername); // registry.add("spring.datasource.password", postgresDB::getPassword); // // } }
Code language: Java (java)

You might be wondering , if we remove all the related annotations how can we use the Testcontainers in our projects.

The magic is in added maven dependencies.

The embedded-postgresql jar consumes some properties and produce some properties , we use those properties to start and stop containers.

The jar consumes following properties via bootstrap.properties file

  • embedded.postgresql.enabled (true|false, default is 'true')
  • embedded.postgresql.reuseContainer (true|false, default is 'false')
  • embedded.postgresql.dockerImage (default is set to 'postgres:9.6.8')
  • embedded.postgresql.waitTimeoutInSeconds (default is 60 seconds)
  • embedded.postgresql.database
  • embedded.postgresql.user
  • embedded.postgresql.password
  • embedded.postgresql.initScriptPath (default is null)
  • embedded.postgresql.startupLogCheckRegex (default is null)

Add the bootstrap.properties file under src/test/resources folder

embedded.postgresql.enabled=true embedded.postgresql.dockerImage=postgres:13.2 embedded.postgresql.database=testdb embedded.postgresql.user=test embedded.postgresql.password=test embedded.postgresql.initScriptPath=ddl.sql
Code language: JavaScript (javascript)


embedded-postgresql uses sensible defaults. You might just need to configure only dockerImage ,database and initScriptPath properties in most cases.

The jar produces following properties

  • embedded.postgresql.port
  • embedded.postgresql.host
  • embedded.postgresql.schema
  • embedded.postgresql.user
  • embedded.postgresql.password

We will use the above properties to form the jdbc url ,user and password of the database.

Add the following properties in application.properties under src/test/resources folder.

If you are using profiles, you need to add the below properties to the corresponding profile file.

spring.datasource.url= jdbc:postgresql://${embedded.postgresql.host}:${embedded.postgresql.port}/${embedded.postgresql.schema} spring.datasource.username= ${embedded.postgresql.user} spring.datasource.password= ${embedded.postgresql.password}
Code language: Java (java)

Now the run the integration testcases from your ide and see the result.

testcontainers starter test results

You can find code for this blog in Github.


testcontainers-spring-boot project covers popular components supported by Testcontainers but not all components are supported.Please check the Github repository to see the supported components in starter project.


Playtika/testcontainers-spring-boot: Container auto-configurations for spring-boot based integration tests (github.com)

Similar Posts