I have Spring Boot Application with using spring-kafka
and spring-kafka-test
. I have below MessageProducer.
@Component
public class MessageProducer {
private KafkaTemplate<String, String> kafkaTemplate;
@Autowired
public MessageProducer(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
public void sendMessage(String message, String topicName) {
kafkaTemplate.send(topicName, message);
}
}
I want to write Junit for above without any mocking the class. I tried with EmbeddedKafkaRule
but I am not sure how to connect it to my application defined kafka broker so when I send message on topic, consumer (in which @kafkaLister
is present) should pick the message and process it.
With EmbeddedKafkaRule
I am also getting below error.
[Producer clientId=producer-1] Connection to node 0 (localhost/192.168.0.24:57516) could not be established. Broker may not be available.
Can some one please let me know how to write a Junit for my kafka producer without mocking any classes and it should test with real classes.