gpt4 book ai didi

java - 在 Spring Boot 中使用 @TestPropertySource 覆盖 @PropertySource

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:19 26 4
gpt4 key购买 nike

我有我的 Spring Boot 主类:

@SpringBootApplication
@PropertySource("file:/my/file/properties")
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
//main method
}

我正在从外部文件中读取属性(使用 @PropertySource)。现在,我有一个集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes= Application.class)
@WebIntegrationTest
@TestPropertySource("file:/my/test/file/properties") // <---
public class MyTest {
//some tests
}

我需要使用另一个外部属性文件,不同于 Application 类中的 @PropertySource 中指定的文件。出于这个原因,我添加了 @TestPropertySource,但似乎此注释并未覆盖 @PropertySource

我能做什么?

提前致谢。

最佳答案

这样使用:

@TestPropertySource(locations = "classpath:test.properties")

并将测试属性文件放入src/test/resources

关于java - 在 Spring Boot 中使用 @TestPropertySource 覆盖 @PropertySource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36614935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com