用于集成测试的Spring-boot默认配置文件 [英] Spring-boot default profile for integration tests

查看:158
本文介绍了用于集成测试的Spring-boot默认配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring-boot使用Spring配置文件( http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html )允许例如为不同的环境分别配置。
我使用此功能的一种方法是配置测试数据库以供集成测试使用。
我想知道是否有必要创建我自己的配置文件'test'并在每个测试文件中明确激活此配置文件?
现在我按以下方式进行:

Spring-boot utilizes Spring profiles (http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html) 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:


  1. 在src / main / resources中创建application-test.properties

  2. 在那里编写测试特定配置(现在只是数据库名称)

  3. 在每个测试文件中包括:

  1. Create application-test.properties inside src/main/resources
  2. Write test specific config there (just the database name for now)
  3. 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注释包括 @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆