我正在尝试使用Spring Boot测试YAML配置文件。以下yaml-props.yml是我的配置文件:
name: def
---
spring:
profiles: live
name: live
---
spring:
profiles: test
name: test此外,我还在application.properties文件中将spring.profiles.active属性设置为live (两个配置文件都在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选择了最后定义的概要文件。这一切为什么要发生?
发布于 2020-01-21 17:44:02
尝试在@SpringBootTest之后添加以下内容。
@SpringBootTest(value={"spring.profiles.active=live"})发布于 2020-03-04 16:04:33
首先,你的配置文件是错误的(注意缩进),它应该是:
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
发布于 2020-01-21 17:17:05
来自Spring文档
文档按照它们遇到的顺序进行合并。较晚的值将覆盖较早的值。
你应该控制你想要使用的配置文件,例如--spring.profiles.active=live
https://stackoverflow.com/questions/59837280
复制相似问题