diff --git a/README.md b/README.md index d50123d..fd94db1 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 Test Container + +`@KafkaTestContainer` Annotation for integration tests. + +Initialize Kafka test container using test container default properties + ## Postgres Test Container `@PostgresTestContainer` Annotation for integration tests. diff --git a/build.gradle.kts b/build.gradle.kts index b64f200..821e7c5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,6 +12,7 @@ dependencies { implementation(hlaeja.springboot.starter.r2dbc) implementation(hlaeja.springboot.starter.test) implementation(hlaeja.testcontainers.junit) + implementation(hlaeja.testcontainers.kafka) implementation(hlaeja.testcontainers.postgresql) testRuntimeOnly(hlaeja.junit.platform.launcher) diff --git a/gradle.properties b/gradle.properties index 7447d9d..fbca745 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ kotlin.code.style=official version=0.4.0-SNAPSHOT -catalog=0.11.0 +catalog=0.12.0-SNAPSHOT diff --git a/src/main/kotlin/ltd/hlaeja/test/container/KafkaTestContainer.kt b/src/main/kotlin/ltd/hlaeja/test/container/KafkaTestContainer.kt new file mode 100644 index 0000000..2d59a27 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/test/container/KafkaTestContainer.kt @@ -0,0 +1,11 @@ +package ltd.hlaeja.test.container + +import ltd.hlaeja.test.container.extension.KafkaTestExtension +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.test.context.ContextConfiguration + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +@ExtendWith(KafkaTestExtension::class) +@ContextConfiguration(initializers = [KafkaTestExtension::class]) +annotation class KafkaTestContainer diff --git a/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaTestExtension.kt b/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaTestExtension.kt new file mode 100644 index 0000000..b8f3d6e --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/test/container/extension/KafkaTestExtension.kt @@ -0,0 +1,21 @@ +package ltd.hlaeja.test.container.extension + +import ltd.hlaeja.test.container.kafka.TestContainerKafka +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 KafkaTestExtension : BeforeAllCallback, ApplicationContextInitializer { + + override fun initialize(applicationContext: ConfigurableApplicationContext) { + TestPropertyValues.of(TestContainerKafka.props()).applyTo(applicationContext.environment) + } + + override fun beforeAll(context: ExtensionContext) { + if (!TestContainerKafka.kafka.isRunning) { + TestContainerKafka.kafka.start() + } + } +} diff --git a/src/main/kotlin/ltd/hlaeja/test/container/kafka/TestContainerKafka.kt b/src/main/kotlin/ltd/hlaeja/test/container/kafka/TestContainerKafka.kt new file mode 100644 index 0000000..a849ee7 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/test/container/kafka/TestContainerKafka.kt @@ -0,0 +1,13 @@ +package ltd.hlaeja.test.container.kafka + +import org.testcontainers.kafka.ConfluentKafkaContainer + +object TestContainerKafka { + + val kafka: ConfluentKafkaContainer = ConfluentKafkaContainer("confluentinc/cp-kafka:8.0.0") + .withReuse(true) + + fun props(): Map = mapOf( + "spring.kafka.bootstrap-servers" to kafka.bootstrapServers, + ) +}