'How to register a ApplicationEnvironmentPreparedEvent in Spring Test

I have a @SpringBootTest and I need to notified via ApplicationEnvironmentPreparedEvent to create a database file if it not exists, because my application database tries to connect to it and it doesn't exists.

I was doing this via SpringApplicationBuilder, but in the JUnit I haven't access to this builder. This my current main code:

SpringApplicationBuilder appBuilder = new SpringApplicationBuilder();
appBuilder.headless(false);
appBuilder.listeners(new ApplicationListener<ApplicationEvent>() {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            Environment env = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
            String datasourceUrl = env.getProperty(RepositoryConfig.JDBC_URL_PROPERTY);

            File db = FirebirdUtil.extractDatabaseFile(datasourceUrl);
            if (db != null) {
                String user = env.getProperty(RepositoryConfig.JDBC_USER_PROPERTY);
                String password = env.getProperty(RepositoryConfig.JDBC_PASSWORD_PROPERTY);

                // this will create the FDB file if it doesn't exists
                FirebirdUtil.createDatabaseifNotExists(db, user, password);
            }
        }
    }
});

How can I be notified when the Enviroment is ready, to read the JDBC URL and create the database file for the test before the datasource configuration?



Solution 1:[1]

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;
    }
}

Solution 2:[2]

Another way is to utilize @Import annotation to inject ApplicationListener bean into application context:

@RunWith(SpringRunner.class)
@SpringBootTest
@Import(ApplicationTests.ApplicationEventListener.class)
public class ApplicationTests {

    public static class ApplicationEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
        @Override
        public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
            // do required staff
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 borino
Solution 2 Lu55