add kafka and postgres test container

This commit is contained in:
2025-08-12 13:28:18 +02:00
committed by swordsteel
parent ca85a7ac64
commit 370d8fb81d
3 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package ltd.hlaeja.test.container
import ltd.hlaeja.test.container.extension.KafkaPostgresTestExtension
import ltd.hlaeja.test.container.postgres.PostgresTestListener
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.TestExecutionListeners.MergeMode
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@ExtendWith(KafkaPostgresTestExtension::class)
@ContextConfiguration(initializers = [KafkaPostgresTestExtension::class])
@TestExecutionListeners(
listeners = [PostgresTestListener::class],
mergeMode = MergeMode.MERGE_WITH_DEFAULTS,
)
annotation class KafkaPostgresTestContainer

View File

@@ -0,0 +1,27 @@
package ltd.hlaeja.test.container.extension
import ltd.hlaeja.test.container.kafka.TestContainerKafka
import ltd.hlaeja.test.container.postgres.TestContainerPostgres
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtensionContext
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
class KafkaPostgresTestExtension : BeforeAllCallback, ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(applicationContext: ConfigurableApplicationContext) {
TestPropertyValues
.of(TestContainerPostgres.props() + TestContainerKafka.props())
.applyTo(applicationContext.environment)
}
override fun beforeAll(context: ExtensionContext) {
if (!TestContainerPostgres.postgres.isRunning) {
TestContainerPostgres.postgres.start()
}
if (!TestContainerKafka.kafka.isRunning) {
TestContainerKafka.kafka.start()
}
}
}