【发布时间】:2022-01-11 07:15:02
【问题描述】:
我有使用 spring-kafka 和 spring-kafka-test 的 Spring Boot 应用程序。我有下面的 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);
}
}
我想为上面编写 Junit 而不模拟类。我尝试使用EmbeddedKafkaRule ,但我不确定如何将其连接到我的应用程序定义的 kafka 代理,因此当我发送有关主题的消息时,消费者(其中存在@kafkaLister)应该选择消息并处理它。
EmbeddedKafkaRule 我也遇到了错误。
[Producer clientId=producer-1] Connection to node 0 (localhost/192.168.0.24:57516) could not be established. Broker may not be available.
谁能告诉我如何在不模拟任何类的情况下为我的 kafka 制作人编写一个 Junit,它应该使用真实的类进行测试。
【问题讨论】:
标签: spring-boot spring-kafka spring-kafka-test