用于集成测试的 Spring-boot 默认配置文件 [英] Spring-boot default profile for integration tests
问题描述
Spring-boot 利用 Spring 配置文件 允许例如为不同的环境有单独的配置.我使用此功能的一种方法是配置测试数据库以供集成测试使用.但是我想知道是否有必要创建我自己的配置文件测试"并在每个测试文件中明确激活此配置文件?现在我是这样做的:
Spring-boot utilizes Spring profiles which allow for instance to have separate config for different environments. One way I use this feature is to configure test database to be used by integration tests. I wonder however is it necessary to create my own profile 'test' and explicitly activate this profile in each test file? Right now I do it in the following way:
在 src/main/resources 中创建 application-test.properties
Create application-test.properties inside src/main/resources
在那里编写测试特定的配置(现在只是数据库名称)
Write test specific config there (just the database name for now)
在每个测试文件中包括:
In every test file include:
@ActiveProfiles("test")
有更聪明/更简洁的方法吗?例如默认测试配置文件?
Is there a smarter / more concise way? For instance a default test profile?
编辑 1:此问题与 Spring-Boot 1.4.1 相关
Edit 1: This question pertains to Spring-Boot 1.4.1
推荐答案
据我所知,没有什么可以直接解决您的请求 - 但我可以提出一个可以帮助的提案:
As far as I know there is nothing directly addressing your request - but I can suggest a proposal that could help:
您可以使用自己的测试注释,即 meta annotation 包含 @SpringBootTest
和 @ActiveProfiles("test")
.因此,您仍然需要专用配置文件,但要避免在所有测试中分散配置文件定义.
You could use your own test annotation that is a meta annotation comprising @SpringBootTest
and @ActiveProfiles("test")
. So you still need the dedicated profile but avoid scattering the profile definition across all your test.
此注释将默认为配置文件 test
,您可以使用元注释覆盖配置文件.
This annotation will default to the profile test
and you can override the profile using the meta annotation.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
@AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}
这篇关于用于集成测试的 Spring-boot 默认配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!