[Spring Boot] Unit Testing Kafka Producer with EmbeddedKafkaBroker

2 min readNov 30, 2021


When working with Kafka, testing can be a little bit tricky because there are many settings to be done in order to start testing. For example, Kafka broker needs to be running, the application needs to point to the correct broker, etc.

This article will cover how to unit test Kafka producer with EmbeddedKafkaBroker.

The benefits of this approach is that

  • Tests are automated
  • There is no need to set up a testing environment or brokers

However, at this point I am not sure how much this approach can cover in terms of different configurations and complex usages of Kafka. Hopefully, there will be more in this series to cover with more robust Kafka examples.

Kafka Producer Implementation

Let us start with the most basic KafkaProducer.

Of course KafkaTemplate can be configured according to the requirements of an application. KafkaAutoConfiguration will create one for you out-of-box pointing at if KafkaTemplate bean is missing. I will create a javaconfig for it. As of right now, it may not do much but I will add more customization to it in future articles.

Test Code Implementation


First of all, the test code needs to be set up in order to use the EmbeddedKafkaBroker. This is important because by doing this, we don’t need to take care of the Kafka brokers during testing.

Because of the fact that SampleProducer makes use of KafkaTemplate and our producer needs to point at the embedded broker instead of, some configuration needs to be done.

Writing Tests

Secondly, tests can be written using the configuration provided above.

It can be noticed that any records sent by SampleProducer goes to EmbeddedKafkaBroker. Then, each message is added to a queue. Lastly, assertions in the test code can be performed on the messages from the queue.



Recommended from Medium


See more recommendations