Spring 启动 - 测试属性被产品属性覆盖
Spring boot - test properties overriden with prod properties
我的测试属性被放置在生产属性中的属性覆盖。
一开始我都命名为 application.yml
但它没有用,所以我按照 中的说明更改为 application-test.yml
并使用配置文件。
现在看起来像下面(kotlin):
@SpringBootTest
@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [InvalidPropertiesApplication::class])
@ActiveProfiles("test")
@TestPropertySource(locations = ["classpath:application.yml"])
class InvalidPropertiesApplicationTests {
@Test
fun contextLoads(@Autowired users: Users) {
assertEquals("TEST", users.file)
}
}
在src/main/resources/application.yml
中我只设置了这个属性
到 PRODUCTION
,在 src/test/resources/application-test.yml
到 TEST
。
并且此测试失败。
可以找到完整示例 at github
提前致谢。
在您的示例中,使用 @TestPropertySource
加载的属性的优先级高于所有其他 属性 源。而 "classpath:application.yml"
指的是 src/main/resources/application.yml
.
顺序:@TestPropertySource > application-{profile}.properties/yaml > application.properties/yaml
我的测试属性被放置在生产属性中的属性覆盖。
一开始我都命名为 application.yml
但它没有用,所以我按照 application-test.yml
并使用配置文件。
现在看起来像下面(kotlin):
@SpringBootTest
@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [InvalidPropertiesApplication::class])
@ActiveProfiles("test")
@TestPropertySource(locations = ["classpath:application.yml"])
class InvalidPropertiesApplicationTests {
@Test
fun contextLoads(@Autowired users: Users) {
assertEquals("TEST", users.file)
}
}
在src/main/resources/application.yml
中我只设置了这个属性
到 PRODUCTION
,在 src/test/resources/application-test.yml
到 TEST
。
并且此测试失败。 可以找到完整示例 at github
提前致谢。
在您的示例中,使用 @TestPropertySource
加载的属性的优先级高于所有其他 属性 源。而 "classpath:application.yml"
指的是 src/main/resources/application.yml
.
顺序:@TestPropertySource > application-{profile}.properties/yaml > application.properties/yaml