I am currently working on a project where we have kafka implementation in micro service architecture. Were you successful in creating contract test cases for mS to kafka topic interaction please using pact-jvm ?
My implementation is microservice1 publishes a message to a REST Client which in turn posts the message to Kafka Topic. microservice2 uses GET method to retrieve messages from the Kafka Topic.
Contract testing is a technique for testing an integration point by isolating each microservice and checking whether the HTTP requests and responses that the microservice transmits conform to a shared understanding that is documented in a contract.
Testing a Kafka Consumer Consuming data from Kafka consists of two main steps. Firstly, we have to subscribe to topics or assign topic partitions manually. Secondly, we poll batches of records using the poll method. The polling is usually done in an infinite loop.
The Pact tool and contract-driven testing The Pact tool provides a way to define the contract between the consumer and the provider in code, along with the tools to test the contract.
To test Kafka APIs, you use the API Connection test step. To add it to a test case, you will need a ReadyAPI Test Pro license. If you do not have it, try a ReadyAPI trial.
Pact-JVM supports Message Pacts, which encapsulate a message that is consumed (one way) over some mechanism, normally a message queue. The idea is to test the consumer code can consume the message via a consumer test, and then verify that the provider generates an appropriate message. The actual message queue is not used in the test.
This was originally developed to apply contract tests for micro-services communicating over a Kafka message queue.
The tests are done in two parts, just like Request-Response Pact tests, except the Consumer reads the message during the consumer pact test and if successful a pact file is written. Then the provider code is called to generate a message, and that is compared to what is in the pact file.
The relevant sections of the Pact-JVM docs are:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With