SpringSpring Kafka Testing
How do you write a Spring Kafka integration test without external Kafka?
Reference answer
Use @EmbeddedKafka: @ExtendWith(SpringExtension.class) @SpringBootTest @EmbeddedKafka(partitions = 1, brokerProperties = {'listeners=PLAINTEXT://localhost:9092', 'port=9092'}, topics={'my-topic'}). Override bootstrap-servers property: @TestPropertySource(properties='spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}'). Produce via KafkaTemplate, listen via @KafkaListener, synchronize via CountDownLatch or Awaitility. The embedded broker runs in the same JVM with no port conflicts between test classes when using random ports (default).
Expected key concepts: @EmbeddedKafka, @SpringBootTest, brokerProperties, @TestPropertySource, spring.embedded.kafka.brokers, CountDownLatch, Awaitility, random ports, same JVM, no port conflicts