Reputation: 91
I am trying to run selenium tests agains my spring-boot app. I want to start the app with the properties my application.yml and application-test.yml define. However, by default this doesn't happen.
I have tried to do as Dave Syer suggested and have implemented an ApplicationContextInitializer that reads the application.yml and application-test.yml files using a YamlPropertySourceLoader.
This doesn't seem to have any effect- setting the server port to 9000 in my application-test has no effect.
Below is my Test Base Class code:
@ContextConfiguration(classes = {TestConfiguration.class}, initializers = {TestApplicationYamlLoaderApplicationContextInitializer.class})
@SharedDriver(type = SharedDriver.SharedType.ONCE)
@ActiveProfiles({"test"})
public abstract class IntegrationBase extends AbstractTestNGSpringContextTests {
....
}
Below is the code for my ApplicationContextInitializer:
public class TestApplicationYamlLoaderApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
YamlPropertySourceLoader loader = YamlPropertySourceLoader.matchAllLoader();
PropertySource applicationYamlPropertySource = loader.load("application.yml", new FileSystemResource("src/main/resources/application.yml"));
PropertySource testProfileYamlPropertySource = loader.load("application.yml", new FileSystemResource("src/main/resources/application-test.yml"));
env.getPropertySources().addFirst(applicationYamlPropertySource);
env.getPropertySources().addFirst(testProfileYamlPropertySource);
System.out.println("woohoo!");
}
}
And the application-test.yml
server:
port: 9000
Upvotes: 4
Views: 11710
Reputation: 333
Using @SpringBootApplication in your main config class, then spring boot will load application.yml automatically. If you want to load applicaton-test.yml, just set the current profile to test. This is an example:
@SpringBootApplication
public class Main {
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
public static void main(String[] args) {
System.setProperty("spring.profiles.active", "test");
SpringApplication.run(Main.class, args);
}
}
You may not have a main method, just put the profile set in any proper place, i.e. JVM start parameters.
Ref. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:
Upvotes: 2
Reputation: 58094
@ContextConfiguration
doesn't know about the Spring Boot initializers. Did you try @SpringApplicationConfiguration
? (Then you wouldn't need your custom initializer.)
Upvotes: 2