Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring引导YAML配置文件配置文件_Spring Boot_Yaml - Fatal编程技术网

Spring boot Spring引导YAML配置文件配置文件

Spring boot Spring引导YAML配置文件配置文件,spring-boot,yaml,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文件夹下。)

我正在尝试用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(“类路径:yaml props.yml”)
公共级YMLUnitTest{
@自动连线
私人YMLProps YMLProps;
@自动连线
私人环境署;
@试验
public void testYmlProps(){
assertEquals(“live”,env.getActiveProfiles()[0]);//活动配置文件为“live”
assertEquals(“live”,ymlProps.getName());//此处失败!
}
}
但是,在第二个断言中,测试失败:

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

Spring似乎选择了最后定义的概要文件。为什么会发生这种情况

来自Spring文档

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


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

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

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

我希望它能起作用

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

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


4.0.0
贝特里斯塔

我在问题中提到,我已经设置了
spring.profiles.active
属性。您是否可以尝试根据配置文件将不常见的属性拆分为两个不同的文件,例如
application test.yml
application live.yml
,然后我认为该属性可以工作。您是否可以共享
pom.xml
文件和
YMLProps