Spring-boot利用弹簧型材 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html它允许针对不同的环境进行单独的配置。
我使用此功能的一种方法是配置集成测试使用的测试数据库。
然而,我想知道:是否有必要创建自己的配置文件“测试”并在每个测试文件中显式激活此配置文件?
现在,我按以下方式进行:
-
里面创建application-test.propertiessrc/main/resources
-
在那里编写特定于测试的配置(现在只是数据库名称)
-
在每个测试文件中,包括:
@ActiveProfiles("test")
有没有更聪明/更简洁的方法?例如,默认的测试配置文件?
编辑1:这个问题与Spring-Boot 1.4.1有关
据我所知,没有什么可以直接解决您的请求 - 但我可以提出一个可以提供帮助的建议:
您可以使用自己的测试注释元注释 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-meta-annotations包括@SpringBootTest
and @ActiveProfiles("test")
。因此,您仍然需要专用配置文件,但避免将配置文件定义分散到所有测试中。
此注释将默认为配置文件test
您可以使用元注释覆盖配置文件。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
@AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)