【问题标题】:Spring boot - test properties overriden with prod propertiesSpring boot - 使用 prod 属性覆盖的测试属性
【发布时间】:2019-03-20 15:00:37
【问题描述】:

我的测试属性被放置在生产属性中的属性覆盖。 一开始我都命名为application.yml,但它不起作用,所以我像this post 中所说的那样更改为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.ymlTEST

这个测试失败了。 完整的例子可以找到at github

提前致谢。

【问题讨论】:

    标签: spring-boot kotlin junit5


    【解决方案1】:

    使用@TestPropertySource 加载的属性的优先级高于示例中的所有其他属性源。而"classpath:application.yml" 指的是src/main/resources/application.yml

    顺序:@TestPropertySource > application-{profile}.properties/yaml > application.properties/yaml

    另请参阅Spring Boot Reference Guide

    【讨论】:

    • 谢谢,我已经看到了这个并做了一些改变,但没有区别......但是,现在它有所帮助,也许是缓存或smth。
    • 但是如果我指定了@TestPropertySource(locations = ["classpath:application.yml"]) 并且两个属性文件都命名为application.yml,那么test 中的那个不应该被解析吗?
    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 2021-09-30
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多