如何在spring-boot配置中加载application.yaml config以进行selenium测试

时间:2014-01-31 15:17:28

标签: spring-boot

我正试图在我的spring-boot应用程序中再次运行selenium测试。 我想用我的application.yml和application-test.yml定义属性启动应用程序。但是,默认情况下不会发生这种情况。

我尝试做Dave Syer suggested并实现了一个ApplicationContextInitializer,它使用YamlPropertySourceLoader读取application.yml和application-test.yml文件。

这似乎没有任何影响 - 在我的应用程序测试中将服务器端口设置为9000无效。

以下是我的测试基类代码:

@ContextConfiguration(classes = {TestConfiguration.class}, initializers = {TestApplicationYamlLoaderApplicationContextInitializer.class})
@SharedDriver(type = SharedDriver.SharedType.ONCE)
@ActiveProfiles({"test"})
public abstract class IntegrationBase extends AbstractTestNGSpringContextTests {
 ....
}

以下是我的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!");
}

}

和application-test.yml

server:
  port: 9000

2 个答案:

答案 0 :(得分:1)

@ContextConfiguration不知道Spring Boot初始化器。你试过@SpringApplicationConfiguration了吗? (那你就不需要你的自定义初始化器。)

答案 1 :(得分:1)

在主配置类中使用@SpringBootApplication,然后spring boot将自动加载application.yml。如果要加载applicaton-test.yml,只需将当前配置文件设置为test。这是一个例子:

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

}

您可能没有main方法,只需将配置文件设置在任何适当的位置,即JVM启动参数。

参考。 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Spring Boot允许您外部化配置,以便在不同环境中使用相同的应用程序代码。您可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置。可以使用@Value注释将属性值直接注入到bean中,通过Spring的Environment抽象访问或通过@ConfigurationProperties绑定到结构化对象。

Spring Boot使用一个非常特殊的PropertySource命令,旨在允许合理地覆盖值。按以下顺序考虑属性:

  1. @TestPropertySource测试注释。
  2. 命令行参数。
  3. 来自SPRING_APPLICATION_JSON的属性(嵌入在环境变量或系统属性中的内联JSON)
  4. ServletConfig init参数。
  5. ServletContext init参数。
  6. 来自java:comp / env。
  7. 的JNDI属性
  8. Java系统属性(System.getProperties())。
  9. 操作系统环境变量。
  10. 一个只有随机属性的RandomValuePropertySource。*。
  11. 打包jar之外的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
  12. 打包在jar中的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)
  13. 打包jar之外的应用程序属性(application.properties和YAML变体)。
  14. 打包在jar中的应用程序属性(application.properties和YAML变体)。
  15. @Configuration类上的@PropertySource注释。
  16. 默认属性(使用SpringApplication.setDefaultProperties指定)。