Spring Boot YAML配置文件配置文件

时间:2020-01-21 08:52:12

标签: spring-boot yaml

我正在尝试使用Spring Boot测试YAML配置文件。以下yaml-props.yml是我的配置文件:

name: def
---
spring:
   profiles: live
   name: live
---
spring:
   profiles: test
   name: test

此外,我在spring.profiles.active文件中将live属性设置为application.properties (两个配置文件都在src/test/resources文件夹下。) 最后,这是我的测试课:

@SpringBootTest
@TestPropertySource("classpath:yaml-props.yml")
public class YMLUnitTest {

    @Autowired
    private YMLProps ymlProps;

    @Autowired
    private Environment env;

    @Test
    public void testYmlProps() {

        assertEquals("live", env.getActiveProfiles()[0]); // Active profile is 'live'
        assertEquals("live", ymlProps.getName()); // Fails here!
    }
}

但是,测试在第二个断言中失败:

org.opentest4j.AssertionFailedError: expected: <live> but was: <test>

似乎Spring选择了最后定义的配置文件。为什么会这样?

3 个答案:

答案 0 :(得分:0)

尝试在@SpringBootTest之后添加以下内容。

@SpringBootTest(value={"spring.profiles.active=live"})

我希望它能起作用!

答案 1 :(得分:0)

首先您的配置文件是错误的(通知缩进),应该是:

name: def
---
spring:
  profiles: live
name: live
---
spring:
  profiles: test
name: test

第二个问题是,您不能在任何地方都可以使用YAML文件,可以在其中使用属性文件,而@TestPropertySource就是这种情况,您不能简单地使用YAML文件...

如果使用application.yml,这很简单,但可能会造成混淆。如果要使用多个YAML文件,建议您使用-Dspring.config.additional-location=classpath:yaml-props.yml

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>springTests-springBootTest</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

YMLProps类

package betlista.springTests.springBootTest;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class YMLProps {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

YMLUnitTest类

package betlista.springTests.springBootTest;

import betlista.springTests.springBootTest.YMLProps;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test,live")
public class YMLUnitTest {

    @Autowired
    private YMLProps ymlProps;

    @Autowired
    private Environment env;

    @Test
    public void testYmlProps() {
        String[] activeProfiles = env.getActiveProfiles();
        Assert.assertEquals("live", activeProfiles[1]); // Active profile is 'live'
        Assert.assertEquals("live", ymlProps.getName()); // Fails here!
    }

}

全部在GitHub上可用:https://github.com/Betlista/SpringTests/tree/master/YMLProps

答案 2 :(得分:-1)

Spring文档中的

YAML文档按照它们遇到的顺序进行合并。以后的值会覆盖以前的值。

您应该控制要使用的配置文件,即--spring.profiles.active=live