为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Spring 集成测试中启动应用程序?

如何在 Spring 集成测试中启动应用程序?

富国沪深 2021-10-17 10:41:11
我需要为我的应用程序创建一个集成测试。我使用@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    ...}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信