SpringSpring Kafka Testing
How do you write a Spring Kafka integration test without external Kafka?
Reference answer
Use the embedded broker from spring-kafka-test: annotate the test class with @EmbeddedKafka(partitions = 1, topics = {'my-topic'}) together with @SpringBootTest (or @SpringJUnitConfig); those annotations are meta-annotated so the context-based broker is used. Since version 3.0.10 the bootstrapServersProperty defaults to spring.kafka.bootstrap-servers, so Spring Boot's auto-configured KafkaTemplate and listeners point at the embedded broker; the broker addresses are also available in the system property spring.embedded.kafka.brokers. You can pass brokerProperties to tune the broker. Since Spring Kafka 4.0 the embedded broker is KRaft only (EmbeddedKafkaKraftBroker), and the docs recommend @DirtiesContext to avoid shutdown race conditions. Produce via KafkaTemplate, consume via @KafkaListener, and have the test wait with a timeout, for example ContainerTestUtils.waitForAssignment before sending, then a CountDownLatch or a BlockingQueue poll with a timeout (Awaitility is another common choice). The broker runs inside the test process, so no Docker or external cluster is needed.
Expected key concepts: @EmbeddedKafka, @SpringBootTest, brokerProperties, bootstrapServersProperty, spring.kafka.bootstrap-servers, spring.embedded.kafka.brokers, EmbeddedKafkaKraftBroker, @DirtiesContext, CountDownLatch, BlockingQueue, waitForAssignment
How did that feel?
Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.