From 370d8fb81d443a0ec35bd1a0cdf199fbe8a03daa Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Tue, 12 Aug 2025 13:28:18 +0200 Subject: [PATCH] add kafka and postgres test container --- README.md | 6 +++++ .../container/KafkaPostgresTestContainer.kt | 18 +++++++++++++ .../extension/KafkaPostgresTestExtension.kt | 27 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/main/kotlin/ltd/hlaeja/test/container/KafkaPostgresTestContainer.kt create mode 100644 src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaPostgresTestExtension.kt diff --git a/README.md b/README.md index fd94db1..424af96 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ In the forge of software development, where annotations ignite, A crucible of testing, common classes to excite. Each annotation examined, with attention to detail and might, Their effects on code behavior, tested through day and night. From mockk objects to test doubles, a toolkit to refine, Developers and testers, their skills to redefine. The Annotation Validator, a sentinel of code integrity true, A library of verification, where testing wisdom shines anew. +## Kafka and Postgres Test Container + +`@KafkaPostgresTestContainer` Annotation for integration tests. + +This combined `@KafkaTestContainer` and `@PostgresTestContainer` + ## Kafka Test Container `@KafkaTestContainer` Annotation for integration tests. diff --git a/src/main/kotlin/ltd/hlaeja/test/container/KafkaPostgresTestContainer.kt b/src/main/kotlin/ltd/hlaeja/test/container/KafkaPostgresTestContainer.kt new file mode 100644 index 0000000..43cbef9 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/test/container/KafkaPostgresTestContainer.kt @@ -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 diff --git a/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaPostgresTestExtension.kt b/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaPostgresTestExtension.kt new file mode 100644 index 0000000..4a93725 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaPostgresTestExtension.kt @@ -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 { + + 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() + } + } +}