使用 <profiles> 激活配置文件时未加载应用程序-test.properties

application-test.properties isn't loaded when activating profiles with <profiles>

我正在尝试使用 new feature from Spring Boot 1.3.0.RELEASE -- 通过配置 spring-boot-maven-plugin:

激活配置文件
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <profiles>
            <profile>test</profile>
        </profiles>
    </configuration>
    <executions>
        <execution>
            <id>start-application</id>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-application</id>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但在这种情况下,我的集成测试开始失败,因为 IllegalArgumentException: Could not resolve placeholder 'spring.mail.host' in string value "${spring.mail.host}"

这个变量在src/main/resources/application-test.properties中定义:

spring.profiles: test
spring.mail.host: 127.0.0.1

我的测试是这样的:

@ContextConfiguration(
    loader = AnnotationConfigContextLoader.class,
    initializers = ConfigFileApplicationContextInitializer.class,
    classes = TestContext.class
)
public class WhenAnonymousUserRegisterAccount extends AbstractTestNGSpringContextTests {

    @Value("${spring.mail.host}")
    private String mailHost;

TestContext 只用另一个 属性 文件定义 PropertySourcesPlaceholderConfigurer

有趣的是,如果我从 application.properties 中删除 <profiles> 并激活配置文件,我的测试有效:

spring.profiles.active: test

所以,当我使用 <profiles> Spring 时,似乎没有将 application-test.properties 文件加载到环境中。

问题:

如果您在 spring-boot-maven-plugin 的配置中指定配置文件,则它们仅在您使用该插件执行应用程序时可用,即通过 运行 mvn spring-boot:run。我怀疑,您的集成测试不是这种情况,因此它不起作用。