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

Pio*_*ski 64 java spring spring-boot

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

  1. 在src/main/resources中创建application-test.properties
  2. 在那里编写测试特定的配置(现在只是数据库名称)
  3. 在每个测试文件中包括:

    @ActiveProfiles("test")
    
    Run Code Online (Sandbox Code Playgroud)

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

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

Mat*_*nkt 74

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

您可以使用自己的测试注释,这是一个包含和的元注释.因此,您仍然需要专用配置文件,但避免在所有测试中分散配置文件定义.@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"};
}
Run Code Online (Sandbox Code Playgroud)

  • 如何使用它来声明注释要使用的多个活动配置文件? (4认同)

Pie*_*nry 42

另一种方法是定义实际测试类将扩展的基类(抽象)测试类:

@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("staging")
public abstract class BaseIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

具体测试:

public class SampleSearchServiceTest extends BaseIntegrationTest{

    @Inject
    private SampleSearchService service;

    @Test
    public void shouldInjectService(){
        assertThat(this.service).isNotNull();
    }
} 
Run Code Online (Sandbox Code Playgroud)

这允许您提取的不仅仅是@ActiveProfiles注释.您还可以想象针对不同类型的集成测试的更专业的基类,例如数据访问层与服务层,或功能专业(常见@Before@After方法等).


小智 31

您可以将application.properties文件放在test/resources文件夹中.在那里你设置

spring.profiles.active=test
Run Code Online (Sandbox Code Playgroud)

这是运行测试时的默认测试配置文件.

  • 如果我创建`src/test/resources/application.properties`文件,则在运行测试时会忽略`src/main/resources/application.properties`内容. (26认同)
  • @ciastek您可以为测试添加`application-test.properties`并仅覆盖您需要的属性. (5认同)
  • @Advicer除非默认属性指定“ spring.profiles.active = test”,否则答案不会被接受。 (2认同)
  • 完全是@OrangeDog-也许您可以使用默认情况下处于活动状态的配置文件“默认”。因此,您可以在test / resources / application-default.properties中添加这样的行(除非您当然已经有src / main / application-default.properties文件:-) (2认同)

Boj*_*vic 12

如果你使用 maven,你可以在 pom.xml 中添加:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <argLine>-Dspring.profiles.active=test</argLine>
            </configuration>
        </plugin>
        ...
Run Code Online (Sandbox Code Playgroud)

然后,maven 应该使用这个参数运行你的集成测试(*IT.java),并且 IntelliJ 也将从这个配置文件激活开始 - 这样你就可以在里面指定所有属性

application-test.yml
Run Code Online (Sandbox Code Playgroud)

并且您不需要“-default”属性。


Dem*_*mel 7

要激活“测试”配置文件,请在 build.gradle 中写入:

    test.doFirst {
        systemProperty 'spring.profiles.active', 'test'
        activeProfiles = 'test'
    }
Run Code Online (Sandbox Code Playgroud)


小智 7

这样做的一种相对的方式(实际上,距离@Compito的原始答案还不到一个星期):

  1. 设置spring.profiles.active=testtest/resources/application-default.properties
  2. 添加test/resources/application-test.properties测试并仅覆盖所需的属性。

  • 我在 Spring Boot 2.4.5 中收到此错误:“从位置‘类路径资源 [application-default.yml]’导入的属性‘spring.profiles.active’在配置文件特定资源中无效 [来源:类路径资源 [应用程序默认.yml]” (3认同)
  • 这是否意味着类路径中的默认“application.properties”也会被解析,然后是“test/resources/application-default.properties”,然后,因为检测到配置文件“test”,所以“test/resources/application- test.properties` 被解析?否则,它不会解决[@Compito的答案](/sf/answers/2778791851/)下评论的@ciastek的问题。 (2认同)

小智 7

您可以将测试特定属性放入src/test/resources/config/application.properties.

此文件中定义的属性将覆盖src/main/resources/application.properties测试期间定义的属性。

有关为何如此有效的更多信息,请查看Spring Boots 文档


aSe*_*emy 6

到 2021 年和 Spring Boot 2.4,我发现的解决方案是有 3 个属性文件

  • src/main/resources/application.yml - 包含应用程序的默认道具
  • src/test/resources/application.yml - 将配置文件设置为“test”,并从“main”导入属性
  • src/test/resources/application-test.yml - 包含特定于测试的配置文件,它将覆盖“主要”

以下是内容src/test/resources/application.yml

# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml
Run Code Online (Sandbox Code Playgroud)

例如,如果src/main/resources/application.yml有内容

ip-address: "10.7.0.1"
username: admin
Run Code Online (Sandbox Code Playgroud)

并且src/test/resources/application-test.yml

ip-address: "999.999.999.999"
run-integration-test: true
Run Code Online (Sandbox Code Playgroud)

然后(假设没有其他配置文件)...

运行测试时,

profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true
Run Code Online (Sandbox Code Playgroud)

并且在正常运行应用程序时

profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>
Run Code Online (Sandbox Code Playgroud)

注意:如果src/main/resources/application.ymlcontains spring.profiles.active: "dev",则不会被覆盖src/test/resources/application-test.yml

  • 奇怪的是,如果没有`src/test/resources/application.yml`文件,`environment.getActiveProfiles()`仍然会返回正确的测试配置文件,但如果我通过`@Value("${spring.profiles"获取活动配置文件.active:}")` 注释,它将为空。 (3认同)

Edu*_*rdo 5

就我而言,根据环境,我有不同的 application.properties,例如:

application.properties (base file)
application-dev.properties
application-qa.properties
application-prod.properties
Run Code Online (Sandbox Code Playgroud)

和 application.properties 包含一个属性 spring.profiles.active 来选择正确的文件。

对于我的集成测试,我在application-test.properties里面创建了一个新文件,test/resources并带有@TestPropertySource({ "/application-test.properties" })注释,这是负责选择 application.properties 我想要的文件,具体取决于我对这些测试的需求