测试时排除ApplicationStartup事件侦听器 [英] Exclude ApplicationStartup Event listener when testing
问题描述
我最近在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屋!