如何在 spring 启动测试中覆盖 application-test.yml?

How to override application-test.yml in spring boot test?

我有 src/main/test/resources/application-test.yml,根据 SpringBootTest 它将加载 application.yml 然后加载应用程序-test.yml。但是我遇到这样一种情况,我只想为一个测试覆盖 application-test.yml 中的某些属性,但其他测试需要使用 application-test.yml 中的属性。我该怎么做?

我尝试使用 @TestPropertySource 注释来覆盖,但它不起作用。

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes= MyApplicationTestApplication.class)
@ActiveProfiles("test")
@DirtiesContext
@TestPropertySource(locations = {"classpath:application-test.yml",
                                    "classpath:file-test.properties"})

再创建一个配置文件并激活它们(顺序很重要)怎么样@ActiveProfiles({"test", "test-override"})

或者您可以使用 System.properties 覆盖,例如在静态块中,在 spring 上下文开始自行加载之前。

感谢评论和回答,只是想补充一下对我有用的

@SpringBootTest(properties = "some.property=localhost:9094") 

Link to doc