我需要为我的应用程序创建一个集成测试。我使用@SpringBootTest(classes = {Application.class})注释来启动它,但它的启动需要时间。那么当我的应用程序准备好时,我如何运行测试?问题出在 kafka 侦听器中:@SpringBootApplicationpublic class Application { @Autowired private KafkaConsumeHandler kafkaConsumeHandler; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @KafkaListener(topics = "${kafka.topics.test}", containerFactory = "kafkaListenerContainerFactory")public void listenRegistred(KafkaMessage consumeKafka) { kafkaConsumeHandler.handleStartProcess(consumeKafka);}如果我尝试在测试中立即发送消息,则侦听器将无法捕捉到它们。所以我在发送之前使用了一点暂停。@RunWith(SpringRunner.class)@SpringBootTest(classes = {Application.class})@DirtiesContextpublic class ProcessTest { @ClassRulepublic static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true, "testTopic");@Testpublic void sendTestRegistred() throws Exception { Thread.sleep(5000); // Need a delay to boot an application ...}
添加回答
举报
0/150
提交
取消