我有使用spring-kafka和spring-kafka-test的。我有低于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 broker,所以当我发送主题消息时,消费者(其中有)应该选择该消息并处理。
使用EmbeddedKafkaRule,我也会得到以下错误。
[Producer clientId=producer-1] Connection to node 0 (localhost/192.168.0.24:57516) could not be established. Broker may not be available.有人能不能让我知道如何为我的卡夫卡制作人编写一个Junit,而不嘲笑任何类,它应该用实际的类进行测试。
发布于 2021-12-06 09:16:00
是的,我尝试了下面的实现,它对我是有效的。你可以试试这个。请告诉我,万一我需要进一步的帮助。
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.the.basic.tech.info.kafka.consumer.Receiver;
import com.the.basic.tech.info.kafka.producer.Sender;
import com.the.basic.tech.info.model.Employee;
@SpringBootApplication
public class SpringKafkaApplication implements CommandLineRunner {
@Autowired
Sender sndr;
@Autowired
Receiver rcvr;
public static void main(String[] args) {
SpringApplication.run(SpringKafkaApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
Employee employee = new Employee("2121", "John", "Dept-A", "3000", "30", "California");
sndr.send(employee);
rcvr.getLatch().await(10000, TimeUnit.MILLISECONDS);
}
}https://stackoverflow.com/questions/70239391
复制相似问题