java - 用于集成测试的 Spring-boot 默认配置文件

标签 java spring spring-boot

Spring-boot 使用 Spring profiles它允许为不同的环境进行单独的配置。 我使用此功能的一种方法是配置集成测试使用的测试数据库。 但是,我想知道:是否有必要创建我自己的配置文件“测试”并在每个测试文件中明确激活此配置文件? 现在,我按以下方式进行:

  1. src/main/resources

    中创建 application-test.properties
  2. 在那里编写特定于测试的配置(现在只是数据库名称)

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

    @ActiveProfiles("test")
    

有没有更聪明/更简洁的方法?例如,默认测试配置文件?

编辑 1:这个问题与 Spring-Boot 1.4.1 相关

最佳答案

据我所知,没有任何内容可以直接解决您的请求 - 但我可以提出一个可以提供帮助的建议:

您可以使用自己的测试注释 meta annotation包括 @SpringBootTest@ActiveProfiles("test")。因此,您仍然需要专用配置文件,但要避免将配置文件定义分散到所有测试中。

此注释将默认为配置文件 test,您可以使用元注释覆盖配置文件。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
  @AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}

关于java - 用于集成测试的 Spring-boot 默认配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39690094/

相关文章:

java - Arraylist 中的无限对象 (Java)

java - 使用 PDShadingType4 Apache pdf box 实现类型 4 阴影

java - JSR 250 方法级别安全性在 Spring MVC 中不起作用

java - 如何使用java从docker容器读取文件

java - 使用多个表单在 View 上提交表单时出现 Spring "Neither BindingResult nor plain target object for bean name"错误

java - Spring Boot 2 - 在初始化 bean 之前做一些事情

java - Netty:不使用证书的安全连接

java - 取消委托(delegate)方法

java - 如何更新@OneToMany 集合中的现有条目?

java - 没有 Setter 方法的 Spring Autowire