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

标签 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

最佳答案

@ContextConfiguration 不知道 Spring Boot 初始值设定项。您尝试过@SpringApplicationConfiguration吗? (那么您就不需要自定义初始值设定项。)

关于spring-boot - 如何在 spring-boot 配置中加载 application.yaml 配置以进行 selenium 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483466/

相关文章:

java - 我如何确定是否使用 Spring jpa 插入或更新记录

java.lang.IllegalStateException : Encountered invalid @Scheduled method 'execute' : For input string: "1#1"

java - Spring Boot 的外部库文件夹

Spring WebSockets ActiveMQ convertAndSendToUser

spring - Spring Security不会发布到提供的登录处理URL

java - 我们可以使用 Spring Boot 对 Nodejs 应用程序进行身份验证吗?

java - PagedResources<Resource 和 RestTemplate 不起作用

java - LocalDateTime 以数组格式表示

java - Spring Boot Embedded Tomcat 依赖在启动时抛出异常

java - 在同一个项目中使用jpa存储库和hibernate(使用标准,HQL调用查询)可以吗