When running individually test will pass, but when running all tests from the test class, test will fail with the vert.x java net connection refused exception. For reference, test class inherits from the base test class that sets up the shared testcontainer. It can be observed from the logs that the docker and the postgres container is successfully started, and liquibase migration is successfully performed.
Base test class is given below:
@TestInstance(value = TestInstance.Lifecycle.PER_CLASS)
@MicronautTest(transactional = false)
abstract class AbstractIT : TestPropertyProvider {
companion object {
@JvmStatic
protected var postgresqlContainer: GenericContainer<*>? = null
}
@Inject
open var application: EmbeddedApplication<*>? = null
override fun getProperties(): MutableMap<String, String> {
return mutableMapOf(
Pair("liquibase.enabled", "true"),
Pair("liquibase.datasources.local.enabled", "true"),
Pair("liquibase.datasources.local.change-log", "classpath:db/migrations/paka-db.changelog-master.xml"),
Pair("liquibase.datasources.local.contexts", "test-data"),
Pair("liquibase.datasources.local.drop-first", "true"),
Pair("datasources.local.jdbc-url", getPostgreSqlUri().replace("postgres", "postgresql")),
Pair("datasources.local.driver-class-name", "org.postgresql.Driver"),
Pair("datasources.local.username", "pakauser"),
Pair("datasources.local.password", "password"),
Pair("jpa.local.reactive", "true"),
Pair("jpa.local.properties.hibernate.jdbc.batch_size", "50"),
Pair("jpa.local.properties.hibernate.id.new_generator_mappings", "true"),
Pair("jpa.local.properties.hibernate.show_sql", "true"),
Pair("jpa.local.properties.hibernate.format_sql", "true"),
Pair("jpa.local.properties.hibernate.highlight_sql", "true"),
Pair("jpa.local.properties.hibernate.generate_statistics", "true"),
Pair("jpa.local.properties.hibernate.physical_naming_strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"),
Pair("jpa.local.properties.hibernate.hbm2ddl.auto", "none"),
Pair("jpa.local.properties.hibernate.connection.url", getPostgreSqlUri()),
Pair("jpa.local.properties.hibernate.connection.username", "pakauser"),
Pair("jpa.local.properties.hibernate.connection.password", "password"),
Pair("jpa.local.properties.hibernate.connection.pool_size", "50"),
Pair("jpa.local.entity-scan.packages", "tech.grgat.paka"),
Pair("jpa.local.entity-scan.enabled", "true"),
)
}
private fun getPostgreSqlUri(): String {
if (postgresqlContainer == null || !postgresqlContainer!!.isRunning) {
startPostgreSql()
}
return "jdbc:postgres://localhost:" + postgresqlContainer!!.getMappedPort(5432) + "/pakadb"
}
private fun startPostgreSql() {
if (postgresqlContainer == null) {
postgresqlContainer = GenericContainer(DockerImageName.parse("postgres:14.4"))
.withEnv("POSTGRES_PASSWORD", "password")
.withEnv("POSTGRES_USER", "pakauser")
.withEnv("POSTGRES_DB", "pakadb")
.withExposedPorts(5432)
// .withReuse(true)
.waitingFor(HttpWaitStrategy().forPort(5432))
.waitingFor(Wait.forLogMessage(".*database system is ready to accept connections.*\\s", 2))
.withStartupTimeout(Duration.ofSeconds(5))
}
if (!postgresqlContainer!!.isRunning) {
postgresqlContainer!!.start()
}
}
@AfterEach
open fun stop() {
postgresqlContainer!!.close()
}
exception generated:
12:24:23.385 [default-nioEventLoopGroup-1-7] INFO t.g.p.a.c.AccountControllerImpl - creating account::Received the following JSON object: SignUpDto(username=admin2, email=admin2@admin.com, password=pass)
12:24:23.419 [default-nioEventLoopGroup-1-7] INFO o.h.r.v.impl.DefaultVertxInstance - HR000002: Vert.x not detected, creating a new instance
12:24:23.479 [default-nioEventLoopGroup-1-7] INFO o.h.r.pool.impl.DefaultSqlClientPool - HR000011: SQL Client URL [jdbc:postgres://localhost:49312/pakadb]
12:24:23.482 [default-nioEventLoopGroup-1-7] INFO o.h.r.p.i.DefaultSqlClientPoolConfiguration - HR000025: Connection pool size: 50
12:24:23.534 [vert.x-eventloop-thread-0] ERROR t.g.p.c.e.GlobalExceptionHandler - unexpected error occurred
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:49312
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:777)
When running test individually it can be observed that the test successfully passes:
12:36:04.107 [default-nioEventLoopGroup-1-3] INFO o.h.r.pool.impl.DefaultSqlClientPool - HR000011: SQL Client URL [jdbc:postgres://localhost:49314/pakadb]
12:36:04.110 [default-nioEventLoopGroup-1-3] INFO o.h.r.p.i.DefaultSqlClientPoolConfiguration - HR000025: Connection pool size: 50
12:36:04.246 [vert.x-eventloop-thread-0] INFO t.g.p.a.facade.AccountFacadeImpl - create account::Entering with: request -> SignUpDto(username=admin2, email=admin2@admin.com, password=pass)
.
.
.
hibernate successfully persists the generated entity
It should also be noted that
transactional=false
should be set on MicronautTest annotation to avoid another type of error:
io.micronaut.transaction.exceptions.CannotCreateTransactionException: Could not open Hibernate Session for transaction
but that's probably because hibernate tries to create a non reactive session. Or not?