In a quakus project, I successfully set up testcontainers using only the following configuration:
%test.quarkus.datasource.driver=org.testcontainers.jdbc.ContainerDatabaseDriver
%test.quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQL9Dialect
%test.quarkus.datasource.url=jdbc:tc:postgresql:11:///dbname?TC_INITSCRIPT=file:my-file.sql
This is amazing how easy it is.
However, as I test the deletion of entities, I'd like to execute the initscript before each test, i.a. start each test with the initial DB.
Any idea how to achieve it?
I also tested by creating the PostgreSQLContainer from the test class in order to manage its lifecycle as I need. The problem is that the container port value is random, I need to set it in quarkus configuration but it doesn't work (it seems to be too late: "Connection to 127.0.0.1:6432 refused"). I did it this way:
@Testcontainers
@QuarkusTest
class MyTest {
class MyPostgreSQLContainer(imageName: String) : PostgreSQLContainer<MyPostgreSQLContainer>(imageName)
companion object {
@Container
val postgres = MyPostgreSQLContainer("postgres:11")
.withDatabaseName("integtest")
.withUsername("postgres")
.withPassword("pwd")
.withExposedPorts(5432)
@BeforeAll
@JvmStatic
internal fun beforeAll() {
postgres.start()
System.setProperty("QUARKUS_DATASOURCE_JDBC_URL", "jdbc:postgresql://127.0.0.1:${postgres.firstMappedPort}/kpi_integtest");
println("BeforeAll init() method called")
}
}
with the application.properties config:
quarkus.datasource.jdbc.url=jdbc:postgresql://127.0.0.1:6432/integtest