I have a file with a function which creates a Kafka Producer and publishes some message on to Kafka topic.
def publishToKafkaTopic(value_str):
producer = KafkaProducer(
bootstrap_servers=KAFKA_SERVERS,
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
try:
ack = producer.send(TOPIC_NAME, key=module, value=value_str)
metadata = ack.get()
return "Success"
except KafkaError as kafka_error:
GLOBAL_LOGGER.error(
"Failed to publish record on to Kafka broker with error %s", kafka_error
)
return "Failed"
Now, I want to test my producer by mocking the KafkaProducer and producer.send(). How do I do that??