Jesper Stark
10/15/2024, 2:28 PMThe driver has not received any packets from the server
.
Does anyone have any good sources on how to properly set this up? Ive created a TestListener
extension to setup a mysql container and then i add this to the test config listener.Emil Kantis
10/15/2024, 3:57 PMthanksforallthefish
10/16/2024, 6:25 AMimport io.kotest.core.extensions.ProjectExtension
import io.kotest.core.project.ProjectContext
import org.testcontainers.containers.JdbcDatabaseContainer
import org.testcontainers.containers.Network
import org.testcontainers.containers.PostgreSQLContainerProvider
object PostgresExtension : ProjectExtension {
private val delegate = lazy {
PostgreSQLContainerProvider().newInstance("14.4")
.withNetwork(Network.SHARED)
.withNetworkAliases("db")
}
private val withNetworkAliases by delegate
override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {
beforeProject()
callback(context)
afterProject()
}
fun beforeProject() {
withNetworkAliases.start()
System.setProperty("spring.datasource.url", "jdbc:postgresql://${withNetworkAliases.connectionString()}")
System.setProperty("spring.datasource.username", withNetworkAliases.username)
System.setProperty("spring.datasource.password", withNetworkAliases.password)
System.setProperty("spring.flyway.schemas", withNetworkAliases.databaseName)
}
fun afterProject() {
if (delegate.isInitialized()) withNetworkAliases.stop()
}
private fun JdbcDatabaseContainer<*>.connectionString() = "$host:$firstMappedPort/$databaseName"
}
we use project extension because those are executed before Spring even starts and we can manipulate the configuration (see all those properties set) with values taken from the container.
DISCLAIMER: I don't want to claim this is the right way, it's honestly not nice to look at, but does it's jobJesper Stark
10/16/2024, 12:02 PMApplicationContextInitializer<ConfigurableApplicationContext>
but then it is not managed by kotest so ill see if i can use what you described.Jesper Stark
10/17/2024, 2:30 PM