Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement contract testing when kafka is involved in microservice architecture?

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.

like image 596
sivaganesh sivakumar Avatar asked Jul 03 '17 11:07

sivaganesh sivakumar


People also ask

What is contract testing in Microservices?

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.

How do you write test cases for Kafka?

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.

Which tool is used for contract driven testing?

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.

How do you test Kafka events?

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.


1 Answers

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.

Contract over a message queue

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.

enter image description here

The relevant sections of the Pact-JVM docs are:

  • Gradle Plugin
  • Maven Plugin
  • JUnit Example
like image 196
Ronald Holshausen Avatar answered Sep 18 '22 15:09

Ronald Holshausen