Skip to main content

Kontainers Spring Boot Integration

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

Kontainers integrates with Spring Boot using Kontainers' JUnit Jupiter extension and Spring Boot property suppliers.

Spring Boot Test

Spring Boot integration tests typically rely on Spring Boot's Test framework and tests are annotated with @SpringBootTest to load the application context. When integration testing with an external system, Spring tests can be annotated with various property sources to configure the application context.

Kontainer's property suppliers are designed to dynamically set system properties exposed by Kontainers for Spring Boot Test to use as it configures the application context.

Database Integration Tests

Kontainers supports configuring Spring Boot for both traditional thread based database connections, like JDBC and Hibernate, and reactive database access using R2DBC.

JDBC Data Sources

To configure a Spring Boot integration test datasource, annotate your integration test with both @SpringBootTest and @DatabaseKontainer. You'll need to supply the database container class as well.

ApplicationTests.kt
@SpringBootTest
@DatabaseKontainer(PostgresKontainer::class)
class ApplicationTests {
// autowired deps and tests here
}

R2DBC Data Sources

To configure a Spring Boot integration test reactive datasource, annotate your integration test with both @SpringBootTest and @DatabaseKontainer. You'll need to supply the database container class as well. In this example, we'll use PostgreSQL:

ApplicationTests.kt
@SpringBootTest
@DatabaseKontainer(PostgresKontainer::class)
class ApplicationTests {
// autowired deps and tests here
}

Kontainers will start a Postgres Kontainer and export the following properties to Spring Boot:

spring.r2dbc.url=${pgKontainer.createR2dbcUrl()}
spring.r2dbc.username=${pgKontainer.getUsername()}
spring.r2dbc.password=${pgKontainer.getPassword()}

If you have any reactive repositories or use R2dbcEntityTemplate, Spring will autowire them to use the @DatabaseKontainer you annotated on your test class.

ApplicationTests.kt
@SpringBootTest
@DatabaseKontainer(PostgresKontainer::class)
class ApplicationTests {
@Autowired
private final lateinit var template: R2dbcEntityTemplate

@Autowired
private final lateinit var fooRepository: ReactiveFooRepository

@Test
fun myTest() {
// use template or repositories, etc
}
}

Database Migrations with Flyway

Kontainers automatically exports Spring Boot properties for Flyway migrations if a @DatabaseKontainer annotation is on the test class so Spring Boot can run you database migrations on context startup.

build.gradle.kts
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
runtimeOnly("org.postgresql:postgresql")

testImplementation("org.flywaydb:flyway-core")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.microkt.kontainers:kontainers-junit5")
testImplementation("io.microkt.kontainers:kontainers-postgresql")
testImplementation("io.microkt.kontainers:kontainers-spring-boot")
}