测试时排除ApplicationStartup事件侦听器 [英] Exclude ApplicationStartup Event listener when testing

查看:242
本文介绍了测试时排除ApplicationStartup事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在SpringBoot项目中添加了ApplicationStartup类

I recently added an ApplicationStartup class to my SpringBoot project

@Component
public class ApplicationStartup
    implements ApplicationListener<ApplicationReadyEvent> { ...

它实现了ApplicationListener.

It implements ApplicationListener.

现在,当我运行与该类无关的旧JUNit测试时,testrunner会尝试运行StartupListener,这在这种情况下都不是必需的.

Now when I run my old JUNit tests that have nothing to do with that class, The testrunner tries to Run my StartupListener, which is neither necessary not appropriate in these cases.

我的测试初始化​​后如何跳过ApplicationListener?

How do I skip the ApplicationListener when my tests initialize?

@RunWith(SpringRunner.class)
@SpringBootTest
public class SubmissionItemManagerTest {...

推荐答案

您可以模拟您的ApplicationStartup

将此声明添加到您的测试用例中:

Add this declaration to your test case:

@MockBean
private ApplicationStartup applicationStartup

这将创建模拟的ApplicationStartup实例,并在测试上下文中将其标记为@Primary,从而替换ApplicationStartup的实际实例.

This will create a mocked instance of ApplicationStartup and mark it as @Primary in your test context thereby replacing the actual instance ofApplicationStartup.

这篇关于测试时排除ApplicationStartup事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆