Kafka Embedded
Unit Testing
Spring Cloud Stream
Stream Processing
Java Programming

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.

Practice system design

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:

xml
1<dependencies>
2    <!-- Spring Cloud Stream -->
3    <dependency>
4        <groupId>org.springframework.cloud</groupId>
5        <artifactId>spring-cloud-starter-stream-kafka</artifactId>
6    </dependency>
7
8    <!-- Embedded Kafka and Kafka test utilities -->
9    <dependency>
10        <groupId>org.springframework.kafka</groupId>
11        <artifactId>spring-kafka-test</artifactId>
12        <scope>test</scope>
13    </dependency>
14</dependencies>

Writing Tests with Embedded Kafka

Configuration

You'll need to configure your application's test context to use Embedded Kafka:

java
1@SpringBootTest
2@EmbeddedKafka(partitions = 1, topics = { "topic1" })
3public class KafkaTests {
4
5    @Autowired
6    private KafkaTemplate<String, String> kafkaTemplate;
7
8    @Autowired
9    private EmbeddedKafkaBroker embeddedKafkaBroker;
10
11    @BeforeEach
12    public void setUp() {
13        // Set the brokers' addresses
14        System.setProperty("spring.kafka.bootstrap-servers", embeddedKafkaBroker.getBrokersAsString());
15    }
16    
17    // Test cases here
18}

Creating a Test

When testing message listeners, inject any relevant components and send messages to the input topic. Then, verify the output as necessary:

java
1@Test
2void testSendMessage() {
3    kafkaTemplate.send("topic1", "Hello Kafka");
4    // Include assertions or verifications
5}

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

FeatureBenefit
IsolationTests do not impact each other or external systems
Embedded EnvironmentRequires no external setup or separate Kafka brokers
Resource UsageHigh memory and CPU requirements in test phase
ConfigurationNeed 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design