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?