spring boot如何使用src / test / resource /中的yaml文件自动装配bean

时间:2016-12-10 05:48:16

标签: java spring spring-mvc spring-boot spring-test

我是Spring boot(版本1.1.8,Spring版本4.0.7)的新手,我需要修复一个单元测试。被测试的类使用另一个具有简单自动配置bean的类。 bean的类基本上寻找一堆键值对,其中键具有特定的前缀。代码本身使用src / main / resources中的yaml文件完美地运行。但是,当我运行测试用例时,此bean为null。如何让测试用例读取src / test / resources目录中的yaml文件?

1 个答案:

答案 0 :(得分:2)

您可以为测试类使用@TestPropertySource注释:

@TestPropertySource(locations = "classpath:your-tets-properties-file.yml")