首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot YAML配置文件配置文件

Spring Boot YAML配置文件配置文件
EN

Stack Overflow用户
提问于 2020-01-21 16:52:12
回答 3查看 174关注 0票数 0

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

代码语言:javascript
复制
name: def
---
spring:
   profiles: live
   name: live
---
spring:
   profiles: test
   name: test

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

代码语言:javascript
复制
@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!
    }
}

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

代码语言:javascript
复制
org.opentest4j.AssertionFailedError: expected: <live> but was: <test>

看起来Spring选择了最后定义的概要文件。这一切为什么要发生?

EN

回答 3

Stack Overflow用户

发布于 2020-01-21 17:44:02

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

代码语言:javascript
复制
@SpringBootTest(value={"spring.profiles.active=live"})
票数 0
EN

Stack Overflow用户

发布于 2020-03-04 16:04:33

首先,你的配置文件是错误的(注意缩进),它应该是:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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类

代码语言:javascript
复制
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类

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2020-01-21 17:17:05

来自Spring文档

文档按照它们遇到的顺序进行合并。较晚的值将覆盖较早的值。

你应该控制你想要使用的配置文件,例如--spring.profiles.active=live

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59837280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档