liquibase initialization with r2dbc config shows how to set up R2DBC with Liquibase. I want to create a test using test containers.
@DataR2dbcTest(properties = [
"spring.datasource.url: jdbc:tc:mysql://mysql/gateway?TC_IMAGE_TAG=8",
"spring.r2dbc.url: r2dbc:tc:mysql://mysql/gateway?TC_IMAGE_TAG=8"])
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ContextConfiguration(classes = [
Application::class,
LiquibaseAutoConfiguration::class // this didn't work either
])
class ApplicationR2dbcTests {
@Autowired lateinit var applicationContext: ApplicationContext
...
}
Combining the knowledge from the following answers:
@Testcontainers
@DataR2dbcTest()
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ContextConfiguration(
classes =
[
Application::class,
LiquibaseAutoConfiguration::class])
class ApplicationR2dbcTests {
@Autowired lateinit var connectionFactory: ConnectionFactory
companion object {
@Container private var mySqlContainer = MySQLContainer("mysql:8")
@JvmStatic
@DynamicPropertySource
fun registerDynamicProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.liquibase.url") {
"jdbc:mysql://${mySqlContainer.host}:${mySqlContainer.firstMappedPort}/${mySqlContainer.databaseName}"
}
registry.add("spring.liquibase.user") {
mySqlContainer.username
}
registry.add("spring.liquibase.password") { mySqlContainer.password }
registry.add("spring.r2dbc.url") {
"r2dbc:mysql://${mySqlContainer.host}:${mySqlContainer.firstMappedPort}/${mySqlContainer.databaseName}"
}
registry.add("spring.r2dbc.username") { mySqlContainer.username }
registry.add("spring.r2dbc.password") { mySqlContainer.password }
}
}
@Test
fun liquibaseChangesMade() {
StepVerifier.create(DatabaseClient
.create(connectionFactory)
.sql("select count(*) from DATABASECHANGELOG")
.map { it -> it.get(0) as Long }
.all()
)
.expectNextMatches { it > 0 }
.verifyComplete()
}
}