Richard Gomez
08/20/2021, 2:17 PMpgContainer
doesn't start until the first test is executed. However, @DataR2dbcTest
tries to configure the Spring context before starting any tests, which obviously fails:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception; nested exception is org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsInitializer$ConnectionFactoryBeanCreationException: Failed to determine a suitable R2DBC Connection URL
Caused by: org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsInitializer$ConnectionFactoryBeanCreationException: Failed to determine a suitable R2DBC Connection URL
I'm going to play around with using the PostgreSQLContainer defined in BaseIntegrationTest
, rather than defining one in my Spec.sam
08/20/2021, 2:27 PMsam
08/20/2021, 2:27 PMsam
08/20/2021, 2:28 PMRichard Gomez
08/20/2021, 2:37 PMDatabaseClient
, so they can be used in Constructor injection
⢠etc.
(A bunch of 'magic', as with most Spring annotations.)sam
08/20/2021, 2:37 PMsam
08/20/2021, 2:37 PMRichard Gomez
08/20/2021, 2:39 PMInitializer : ApplicationContextInitializer<ConfigurableApplicationContext>
, which runs before anything.Richard Gomez
08/20/2021, 2:40 PMRichard Gomez
08/20/2021, 2:40 PMsam
08/20/2021, 2:41 PMsam
08/20/2021, 2:41 PMsam
08/20/2021, 2:42 PMsam
08/20/2021, 2:42 PMcompanion object {
private val POSTGRES_CONTAINER = PostgreSQLContainer<Nothing>("postgres:latest")
init {
POSTGRES_CONTAINER.start()
}
}
sam
08/20/2021, 2:42 PMRichard Gomez
08/20/2021, 2:43 PMRichard Gomez
08/20/2021, 2:44 PMsam
08/20/2021, 2:45 PMsam
08/20/2021, 2:45 PMsam
08/20/2021, 2:45 PMRichard Gomez
08/20/2021, 2:47 PMRichard Gomez
08/20/2021, 5:13 PM@ContextConfiguration
annotation initializer:
@DataR2dbcTest
@ContextConfiguration(initializers = [BaseIntegrationTest.Initializer::class])
internal class R2dbcTest(@Autowired private val dbClient: DatabaseClient) : DescribeSpec({
Or Kotest's ProjectConfig (based on Kotest#1649 and "Using Testcontainers with Micronaut and Kotest"):
object ProjectConfig : AbstractProjectConfig() {
override fun beforeAll() {
TestDbContainer.start()
}
override fun afterAll() {
TestDbContainer.stop()
}
}
@DataR2dbcTest
internal class R2dbcTest(@Autowired private val dbClient: DatabaseClient) : DescribeSpec({
Richard Gomez
08/20/2021, 5:17 PMsam
08/20/2021, 5:22 PMsam
08/20/2021, 5:24 PMRichard Gomez
08/20/2021, 5:47 PMI guess DataR2dbcTest requires the instance up and runnning before the class is created,Knowing Spring, there's likely a(n esoteric) method to defer this until after the class is created. That said, using a listener is much easier. This could likely be improved, but it starts/stops test containers only for integration tests and not unit tests.
sam
08/20/2021, 5:47 PMRichard Gomez
08/20/2021, 5:48 PM@DataR2dbcTest
directly.Richard Gomez
08/20/2021, 5:49 PMsam
08/20/2021, 5:51 PMVictor Cardona
06/24/2022, 5:16 PMclass TestDbContainer : PostgreSQLContainer<TestDbContainer>("postgres:13") {
companion object {
private lateinit var instance: TestDbContainer
fun start() {
if (!Companion::instance.isInitialized) {
instance = TestDbContainer()
instance.start()
System.setProperty("DB_URL", instance.jdbcUrl)
System.setProperty("DB_USERNAME", instance.username)
System.setProperty("DB_PASSWORD", instance.password)
}
}
fun stop() {
instance.stop()
}
}
}
---
spring:
datasource:
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
Thanks!Richard Gomez
06/29/2022, 5:50 PM@DataR2dbcTest
@Import(DatabaseConfig::class)
internal class DatabaseIT : IntegrationTest, DescribeSpec({ ...
I wish I remembered why ā I think it had something to do with the properties only being set/respected when called as a part of the ApplicationContextInitializer
hook.Victor Cardona
06/30/2022, 2:17 PM