Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka: Are there are examples on how to use Mockito for unit testing Kafka?

I have a producer application that needs unit testing. I don't want to spin up a Zookeeper and Kafka server for this purpose. Is there a simpler way to test it using Mockito?

like image 305
NoName Avatar asked Mar 21 '18 22:03

NoName


2 Answers

If you don't want to start Kafka and Zookeeper, you can use the Mock clients that come with Kafka to fake sending and receiving messages from a Kafka cluster:

  • MockProducer: http://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/MockProducer.html
  • MockConsumer: http://kafka.apache.org/10/javadoc/org/apache/kafka/clients/consumer/MockConsumer.html
like image 107
Mickael Maison Avatar answered Nov 15 '22 06:11

Mickael Maison


For such testing I've used EmbeddedKafka from the spring-kafka-test library (even though I wasn't using Spring in my app, that proved to be the easiest way of setting up unit tests). Here's an example : https://www.codenotfound.com/spring-kafka-embedded-unit-test-example.html

It actually spins up a Kafka and Zookeeper in the same process for you, so you're not really mocking anything out and so you don't need mockito for this. I used plain JUnit.

like image 31
Michal Borowiecki Avatar answered Nov 15 '22 05:11

Michal Borowiecki