This is all you need to set up PostgreSQL container inside Spring Boot Test with JUnit 5.
@SpringBootTest
@Testcontainers
internal class CatControllerTest {
companion object {
@Container
@JvmStatic
val postgreSQLContainer = PostgreSQLContainer()
@DynamicPropertySource
@JvmStatic
fun postgreProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl)
registry.add("spring.datasource.username", postgreSQLContainer::getUsername)
registry.add("spring.datasource.password", postgreSQLContainer::getPassword)
}
}
}
dependencies {
implementation(platform("org.testcontainers:testcontainers-bom:1.19.5"))
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:postgresql")
}