Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have KafkaProducer to use a mock Schema Registry for testing?

I'm using KafkaProducer in my test cases and my producer uses the schemaRegistryUrl which points a my local instance of Schema Registry. Is there a way to mock how KafkaProducer connects with the Schema Registry? That is, to have KafkaProducer/Consumer in my tests to work without a running instance of Schema Registry.

like image 458
Glide Avatar asked Sep 23 '16 22:09

Glide


People also ask

Can we use Kafka without schema registry?

You can test whether your application has received data from the topic. But if your application uses Apache Avro for serialization, and you do not use Confluent Schema Registry (which is the case when you want to use spring-kafka-test for testing) then you have a problem.

Can I use the schema registry in Apache Kafka?

Using the Schema Registry Now that the Schema Registry is up and running, you can now use it in your applications to store data schemas for your Kafka topics. The following example is a Java application that uses the Schema Registry and Apache Avro to produce and consume some simulated product order events.

How does schema registry work in Kafka?

Schema Registry lives outside of and separately from your Kafka brokers. Your producers and consumers still talk to Kafka to publish and read data (messages) to topics. Concurrently, they can also talk to Schema Registry to send and retrieve schemas that describe the data models for the messages.


1 Answers

https://github.com/confluentinc/schema-registry/blob/master/avro-serializer/src/main/java/io/confluent/kafka/serializers/AbstractKafkaAvroSerDe.java

In 5.3.x, you can find a MOCK_URL_PREFIX = "mock://", so just set the test schemaRegistryUrl with the prefix "mock://", like: "mock://testurl".

like image 200
Gary Pan Avatar answered Sep 18 '22 19:09

Gary Pan