【问题标题】:Activate profiles for spring boot test using application.properties file使用 application.properties 文件为 Spring Boot 测试激活配置文件
【发布时间】:2019-01-05 04:21:56
【问题描述】:

我一直在为我的测试框架使用 Spring Boot 和 TestNG,到目前为止,我的测试被配置为仅使用 src/main/resource 下的一个默认 application.properties 文件。现在我想为不同的环境配置它们 - ci/stage 等。我使用 spring 文档来激活 pom.xml 文件中的配置文件。

 <profiles>
    <profile>
        <id>ci</id>
        <properties>
            <activeProfile>ci</activeProfile>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

我在 src/main/resources 下有两个属性文件 - application.properties 和 application-ci.properties。 (这是spring文档建议的命名约定。application-{activatedprofile}.properties)。

application.properties 有一个占位符 -

spring.profiles.active=@activeProfile@

@activeProfile@ 将被 pom.xml 文件中的 activeProfile 值替换。并且直到它正在工作。

在我的@Configuration 类中,我有如下注释,我希望 ${spring.profiles.active} 值被替换为值 - ci。

 @PropertySource("classpath:application-${spring.profiles.active}.properties")

我收到以下错误:

java.lang.IllegalArgumentException: Could not resolve placeholder 
'spring.profiles.active' in value 
"classpath:application-${spring.profiles.active}.properties"

我正在使用 maven 和 testng 来运行我的项目。我做错了什么,请告诉我如何解决。

【问题讨论】:

  • 如何将@activeProfile@ 替换为代码中的测试?实际上对于测试,您可以使用 @ActiveProfiles("ci")@SpringBootTest
  • 你应该防止在你的 Spring Boot 应用程序中使用 Maven 配置文件。否则我会把它当作代码味道......
  • Maven 配置文件!= Spring 配置文件。此外,您不应该使用这样的配置文件,因为这会导致针对不同环境的不同构建。您不想这样做(因为这基本上意味着您将在未经测试的情况下进行生产)。在您的@PropertySource 中,您还试图超越 Spring Boot(它已经为您完成了所有这些,所以您不需要它)。只需在应用程序启动期间传递您希望使用的配置文件(或通过环境变量或简单地通过 @ActiveProfiles 在您的测试类中。
  • @m-deinum 只是为了澄清如果我在“@Configuration”类上使用“@ActiveProfiles("ci")”,那么测试将选择 application-ci.properties 文件并替换所有属性来自测试中的文件?

标签: java spring maven spring-boot testng


【解决方案1】:

首先,maven配置文件和spring配置文件不一样。在提供的代码 sn-p 中,您设置的是 maven 配置文件,而不是 spring 配置文件。

要在测试阶段传递特定的弹簧配置文件,您可以使用 surefire 插件。在下面的代码 sn-p 中,您将传递系统属性 spring.profiles.active 作为 ci。这相当于在您的application.properties 文件中设置值。

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
      <systemPropertyVariables>
        <spring.profiles.active>ci</spring.profiles.active>
      </systemPropertyVariables>
    </configuration>
  </plugin>

其次,spring 会根据活动的 spring 配置文件自动加载属性源。在您的示例中,spring 将首先加载application.properties,然后在其上应用application-ci.properties。结果

@PropertySource("classpath:application-${spring.profiles.active}.properties")

不需要。

如果您有一个特定于活动配置文件的配置类,那么您可以将 @ActiveProfiles("ci") 添加到您的配置类中,它只会在配置文件 ci 处于活动状态时使用该类。

最后,您不需要 spring.profiles.active=@activeProfile@ 文件中的 spring.profiles.active=@activeProfile@ 属性,因为它是从 maven 中的 surefire 插件传入的。

【讨论】:

  • 还是不行。那么 spring.profiles.active 是否适用于测试项目?我进行了更改,现在在 maven surefire 插件下有了 systemproperty 变量。删除了“@PropertySource”删除了“@activeProfile@”并删除了 Maven 配置文件设置。仍然从 src/test/resources 文件夹中选择 application.properties。
  • application.properties 将始终被选中,但它也应该选择 application-ci.properties 并将值加载到 application.properties 之上。因此,如果在两个属性文件中都设置了属性prop.a,那么在 ci 属性中设置的值将胜出。
  • 由于某种原因仍然无法正常工作,但我会继续尝试并在此处发布解决方案
  • 可以分享你的git项目吗?
猜你喜欢
  • 1970-01-01
  • 2019-11-09
  • 2016-02-12
  • 1970-01-01
  • 2017-04-12
  • 2019-01-19
  • 2017-03-30
  • 2015-05-23
  • 2017-02-05
相关资源
最近更新 更多