Java 如何使用junit为springboot应用程序中的嵌入式kafka编写测试用例
我不知道如何使用junit mockito为springboot应用程序中的嵌入式kafka编写测试用例,我参考了一些代码,并使用了这些代码,但是Kafkamebedded Kafkamebedded不推荐使用Java 如何使用junit为springboot应用程序中的嵌入式kafka编写测试用例,java,spring-boot,unit-testing,junit5,embedded-kafka,Java,Spring Boot,Unit Testing,Junit5,Embedded Kafka,我不知道如何使用junit mockito为springboot应用程序中的嵌入式kafka编写测试用例,我参考了一些代码,并使用了这些代码,但是Kafkamebedded Kafkamebedded不推荐使用 @TestConfiguration @EmbeddedKafka(分区=1,controlledShutdown=false, brokerProperties={“侦听器=明文://localhost:3333”,“端口=3333”}) 公共类KafkaTestConfig{ @自动
@TestConfiguration
@EmbeddedKafka(分区=1,controlledShutdown=false,
brokerProperties={“侦听器=明文://localhost:3333”,“端口=3333”})
公共类KafkaTestConfig{
@自动连线
卡夫卡密德卡夫卡密德;
@自动连线
KafkaListenerEndpointRegistry KafkaListenerEndpointRegistry;
@以前
public void setUp()引发异常{
对于(MessageListenerContainer MessageListenerContainer:KafkListenerEndpointRegistry.getListenerContainers()){
ContainerTestUtils.waitForAssignment(messageListenerContainer,
kafkamebedded.getPartitionsPerTopic());
}
}
@豆子
公共生产工厂生产工厂(){
返回新的默认kafkaproducerfactory(KafkaTestUtils.producerProps(kafkamebedded));
}
@豆子
公共卡夫卡模板卡夫卡模板(){
KafkaTemplate KafkaTemplate=新的KafkaTemplate(producerFactory());
返回卡夫卡模板;
}
}
编写测试时您面临的问题是什么?谢谢,我更新了查询。请检查并帮助我解决此问题
@TestConfiguration
@EmbeddedKafka(partitions = 1, controlledShutdown = false,
brokerProperties = {"listeners=PLAINTEXT://localhost:3333", "port=3333"})
public class KafkaTestConfig {
@Autowired
KafkaEmbedded kafkaEmbeded;
@Autowired
KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
@Before
public void setUp() throws Exception {
for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry.getListenerContainers()) {
ContainerTestUtils.waitForAssignment(messageListenerContainer,
kafkaEmbeded.getPartitionsPerTopic());
}
}
@Bean
public ProducerFactory<String, Object> producerFactory() {
return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(kafkaEmbeded));
}
@Bean
public KafkaTemplate<String, Object> kafkaTemplate() {
KafkaTemplate<String, Object> kafkaTemplate = new KafkaTemplate<>(producerFactory());
return kafkaTemplate;
}
}