add kafka test container

This commit is contained in:
2025-08-12 13:12:18 +02:00
committed by swordsteel
parent 2328e7ebe2
commit ca85a7ac64
6 changed files with 53 additions and 1 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -1,3 +1,3 @@
kotlin.code.style=official
version=0.4.0-SNAPSHOT
catalog=0.11.0
catalog=0.12.0-SNAPSHOT

View File

@@ -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

View File

@@ -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<ConfigurableApplicationContext> {
override fun initialize(applicationContext: ConfigurableApplicationContext) {
TestPropertyValues.of(TestContainerKafka.props()).applyTo(applicationContext.environment)
}
override fun beforeAll(context: ExtensionContext) {
if (!TestContainerKafka.kafka.isRunning) {
TestContainerKafka.kafka.start()
}
}
}

View File

@@ -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<String, String> = mapOf(
"spring.kafka.bootstrap-servers" to kafka.bootstrapServers,
)
}