How to register a ApplicationEnvironmentPreparedEvent in Spring Test

隐身守侯 提交于 2019-12-02 04:26:10

Because in test the main method did not run, that why your listeners are not available in the Test.
First you needs extract listener to the class for future using in test (for exmpl. MyListener).
Second using custom loader for declare listeners in the application.

I just check it works for me. That is example for test:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(loader = CustomLoader.class)
public class DemoApplicationTests {

public static class CustomLoader extends SpringBootContextLoader {

    @Override
    protected SpringApplication getSpringApplication() {
        SpringApplication app = super.getSpringApplication();
        app.addListeners(new MyListener());
        return app;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!