【发布时间】:2020-05-07 06:55:36
【问题描述】:
我正在尝试使用 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 似乎选择了最后定义的配置文件。为什么会这样?
【问题讨论】:
-
你能分享你的
pom.xml文件和YMLProps吗?
标签: spring-boot yaml