Note
The built-in support for Kafka is recommended instead of this approach below. The advantage is that you don't need to generate or compile extra Java code.
This sample project demonstrates how you can test Kafka messaging if your implementation is in Java. It also demonstrates concepts you can use for any case where you use Java interop to connect Karate to any custom code, library or communication protocol.
When you use well designed utility classes, your Karate tests will be clean and only focus on making a call and what data is sent and received.
For example if you look at kafka.feature
the test is just a few lines. Behind the scenes a Kafka consumer and producer is initialized and the call is made. The HTML report even includes details of the call and the messages on the wire, which you can easily customize.
Rather than attempt a generic and dynamic approach, this approach shown here can re-use your existing Kafka implementation code-base and domain classes.
This example also shows how to handle Avro for both serializing and deserializing of messages. You should be able to easily write a conversion from your Avro or Java payloads to JSON and vice-versa.
- Docker is required to start the Kafka broker and Zookeeper. There is a
docker-compose.yml
file in this project. - Run the following commands
docker-compose up -d
mvn test
- To stop the Docker containers run
docker-compose down
- Karate Official Support for Kafka - recommended
- community-example - a community project that provides a generic approach to testing Kafka with Karate