在SpringBoot测试中加载不同的application.yml
我正在使用运行我的src / main / resources / config / application.yml的spring boot应用程序。
当我通过以下方式运行测试用例时:
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class MyIntTest{
}
测试代码仍然运行我的application.yml文件以加载属性。我想知道在运行测试用例时是否可以运行另一个* .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中覆盖它们。
以上是 在SpringBoot测试中加载不同的application.yml 的全部内容, 来源链接: utcz.com/qa/433819.html