小编典典

在SpringBoot测试中加载不同的application.yml

spring-boot

我正在使用运行我的src / main / resources / config / application.yml的spring boot应用程序。

当我通过以下方式运行测试用例时:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class MyIntTest{
}

测试代码仍然运行我的application.yml文件以加载属性。我想知道在运行测试用例时是否可以运行另一个* .yml文件。


阅读 1428

收藏
2020-05-30

共1个答案

小编典典

一种选择是使用配置文件。创建一个名为 application-test.yml
的文件,将这些测试所需的所有属性移到该文件,然后将@ActiveProfiles注释添加到测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@ActiveProfiles("test") // Like this
public class MyIntTest{
}

请注意,它将另外加载application-
test.yml,因此application.yml中的所有属性仍将被应用。如果您不希望这样做,也可以为它们使用配置文件,或者在application-
test.yml中覆盖它们。

2020-05-30