Simple embedded Kafka test example with spring boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Embedded Kafka is useful when you want an integration-style test for producer and consumer logic without standing up an external broker manually. In Spring Boot, the common pattern is to use spring-kafka-test, annotate the test with @EmbeddedKafka, and wire the embedded broker address into spring.kafka.bootstrap-servers.
Dependencies and Test Setup
At minimum, include the Kafka starter and the Kafka test dependency.
Spring Boot's Kafka reference documentation shows two important details for tests:
- use
@EmbeddedKafka - point the embedded broker addresses at
spring.kafka.bootstrap-servers
A Minimal Producer and Listener
First, define a tiny component that sends messages:
Then define a listener that captures the received message:
This is intentionally simple. It gives the test something concrete to assert.
Embedded Kafka Test Class
Now create the integration test:
The key annotation attribute is bootstrapServersProperty. It lets Spring Boot auto-configuration use the embedded broker instead of a real external cluster.
Why This Works
The embedded broker runs inside the test lifecycle, so your KafkaTemplate and @KafkaListener talk to a real Kafka broker instance, just one that happens to be local to the test process.
That makes the test stronger than a pure mock-based test because you exercise:
- topic creation
- serialization and deserialization wiring
- producer send path
- consumer listener wiring
It is still not a full production-cluster test, but it is an excellent middle ground.
Useful Variations
If your test does not need the full Spring Boot context, you can build narrower tests with Spring Kafka test utilities. If you do need to read raw records directly, KafkaTestUtils can help create consumer properties and poll records from the embedded broker.
For more complex systems, consider whether your test should verify:
- listener behavior only
- producer behavior only
- full producer-to-consumer flow through the application
The setup should match the question the test is supposed to answer.
Common Pitfalls
The biggest pitfall is forgetting to map the embedded broker address into spring.kafka.bootstrap-servers. Then the application context may still try to connect to a real broker or to a missing default address.
Another pitfall is making the assertion before the listener has had time to consume the message. Kafka tests are asynchronous by nature, so use a latch or polling helper instead of assuming immediate delivery.
A third pitfall is overloading one integration test with every Kafka scenario in the application. Keep embedded broker tests focused so they stay readable and reasonably fast.
Finally, remember that embedded Kafka is great for application integration tests, but it is not a perfect substitute for environment-level testing against infrastructure that resembles production.
Summary
- Use
spring-kafka-testand@EmbeddedKafkafor local Kafka integration tests in Spring Boot - Map the embedded broker into
spring.kafka.bootstrap-servers - A small
KafkaTemplateproducer plus@KafkaListenerconsumer is enough for a good end-to-end example - Use latches or similar synchronization because message delivery is asynchronous
- Embedded Kafka is ideal for application-level integration testing, not for replacing every production-like test

