How to test event listeners defined in SpringBootApplication

Viewed 1790

Trying to write test of SpringBootApplication:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
@TestPropertySource(locations="classpath:application-test.properties")
public class PropertiesConverterTest { ...

Application class:

@SpringBootApplication
@PropertySource("classpath:/application.properties")
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners(new MyListener1());
        application.addListeners(new MyListener2());
        application.run();
    }
}

But it seems to be that no one of the listeners is been started. How should that type of function be tested? Is it possible at all?

If to be more specific, I want to test properties modification feature implemented in one of the listeners (ApplicationEnvironmentPreparedEvent listener). It actually works, I just want to write a simple test. My idea is to load ApplicationContext and just try to read modified properties using @Value annotation or environment.getProperty("my.property.name")

0 Answers
Related