How to create unit test with kafka embedded in the spring cloud stream
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Testing is a crucial part of developing robust applications, ensuring that each part of your system behaves as expected. For applications that use Apache Kafka through Spring Cloud Stream, incorporating tests with an embedded Kafka broker can streamline development and simplify your testing pipeline. Here, we delve deep into the creation of unit tests with Embedded Kafka in a Spring Cloud Stream environment.
Introduction to Spring Cloud Stream and Embedded Kafka
Spring Cloud Stream is a framework for building message-driven microservices, and it provides a flexible programming model built upon the message broker of your choice, like Apache Kafka. Embedded Kafka is a feature provided by Kafka itself, allowing Kafka brokers to run within a test harness. When combined, these technologies enable you to run stream-processing applications that react to events and facilitate comprehensive testing without needing a full Kafka installation.
Setting Up Your Project
To start, ensure that your Spring Boot project is set up with the necessary dependencies. You will need both spring-kafka-test for the embedded Kafka broker and spring-cloud-stream dependencies. Here's an example of what your pom.xml might include:
Writing Tests with Embedded Kafka
Configuration
You'll need to configure your application's test context to use Embedded Kafka:
Creating a Test
When testing message listeners, inject any relevant components and send messages to the input topic. Then, verify the output as necessary:
Advantages of Using Embedded Kafka for Testing
- Isolation: Tests do not interfere with each other or with an external system.
- Speed: Faster setup and tear down time compared to using an external broker.
- Consistency: More consistent results by having a controlled, predictable environment.
Challenges and Considerations
- Resource Intensive: Embedded Kafka can consume a significant amount of resources; suitable hardware or CI environment is necessary.
- Configuration Complexity: Ensuring that the Kafka configuration matches between test and production environments to avoid configuration-specific failures.
Summary Table
| Feature | Benefit |
| Isolation | Tests do not impact each other or external systems |
| Embedded Environment | Requires no external setup or separate Kafka brokers |
| Resource Usage | High memory and CPU requirements in test phase |
| Configuration | Need to match production settings for reliability |
Conclusion
Unit tests with embedded Kafka provide a powerful way to ensure that your Spring Cloud Stream applications behave correctly before they interact with real Kafka brokers. While resource usage and configuration overhead are concerns, the benefits of using an embedded system—especially in terms of isolating tests and speeding up development cycles—are significant. As modern applications increasingly rely on real-time event processing, such testing strategies become critical in maintaining robust and fault-tolerant systems.
Related reading
- How to create ZeroMQ socket suitable both for sending and consuming?
- How to deal with Kafka warning Error while loading kafka-streams-version.properties java.lang.NullPointerException inStream parameter is null
- How to decide Kafka Cluster size
- How to decide the concurrency to be set in spring kafka?
- How to customise the Jackson JSON mapper implicitly used by Spring Boot?
- How to customize DefaultHandlerExceptionResolver logic?
- How to debug Jest has detected the following ... open handle potentially keeping Jest from exiting
- How to disable Eureka and Spring Cloud Config in a WebMvcTest?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.