Spring Boot:如何覆盖单元测试中的默认属性

Ben*_*rer 4 java spring unit-testing spring-boot spring-boot-test

我尝试为单元测试加载第二个属性文件,这会覆盖一些属性。

@PropertySource用a加载它@Configuration不起作用,用 a 加载它@TestPropertySource也不起作用。仅properties直接设置@TesPropertySource有效,但当我尝试将其变成元注释时它不起作用。

这是一个示例项目:https ://github.com/cptwunderlich/SpringTestProperties

我更喜欢加载一个文件来影响所有测试(例如 with @PropertySource),但如果这不起作用,至少有一个自定义元注释会很好,所以我不必将其放在每个测试上测试。基本上我不想一些数据导入到数据库中进行测试(spring.datasource.data),然后还更改使用的数据库 - 无需复制整个配置并且每次都必须在两个位置更改它。

重要的部分:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
public class TestconfigApplicationTests {

    @Value("${my.test.property}")
    private String testproperty;

    @Test
    public void assertValue() {
        Assert.assertEquals("foobar", testproperty);
    }

}
Run Code Online (Sandbox Code Playgroud)

或者测试包中的配置类:

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@PropertySource("classpath:application-test.properties")
public class GlobalTestConfig {
}
Run Code Online (Sandbox Code Playgroud)

更新:

答案中的主要建议是使用@ActiveProfile激活“测​​试”配置文件,这将导致加载“application-test.yaml”。这比 更好@TestPropertySource,但我仍然需要在每个测试类上添加注释。我尝试创建一个元注释 - 它应该可以工作- 所以至少我只有一个自定义注释,我可以在其中捆绑其他设置。但这是行不通的。

完美的解决方案是使用一个配置类全局设置这些设置,而不必在每个测试上添加注释。我仍在寻找该解决方案,或者至少在关闭此问题之前调试元注释。编辑:我创建了一个 Jira 问题:SPR-17531

编辑

好吧,我有点困惑,所以我重新测试了所有不同的组合:

  • @TestPropertySource(locations = "classpath:application-test.properties")在测试中,现在实际上可以工作了。呵呵。
  • @ActiveProfiles("test")关于测试作品。
  • 元注释with@ActiveProfiles不起作用编辑:它确实...
  • 任何类型的全局配置(TestPropertySource、ActiveProfiles、Propertysource)都不起作用
  • (在测试/资源中拥有 application.properties 也不起作用,BC。它不会覆盖单个属性,而是覆盖完整的文件,即我需要重新定义和复制所有内容。)

编辑:

好吧,我的错。元注释确实有效 - 我忘记设置保留策略,默认为 CLASS。添加@Retention(RUNTIME)修复了这个问题。

似乎没有办法在代码中全局设置它(即,无需在我的 IDE 中配置测试的运行方式),所以我现在必须使用配置文件。

aur*_*ius 5

您可以使用@ActiveProfiles("test")。这会将属性设置application-test.yml到测试环境中。

\n\n
@RunWith(SpringRunner.class)\n@SpringBootTest\n@ActiveProfiles("test")\npublic class TestconfigApplicationTests {\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我们需要针对不同的环境,Boot 中有一个内置机制,因此不需要额外的库或重构。

\n\n

我们可以简单地在目录 \xe2\x80\x93 中定义一个application-environment.properties文件src/main/resources,然后设置一个具有相同环境名称的 Spring 配置文件。

\n\n

例如,如果我们定义一个stagingortest环境,这意味着我们\xe2\x80\x99ll 必须定义一个 staging 或测试配置文件,然后定义application-staging.propertiesor application-test.properties

\n\n

env文件将被加载,并将优先于默认属性文件application.properties. 请注意,默认文件仍将被加载,它\xe2\x80\x99s 只是当存在属性冲突时,环境特定的属性文件优先,这意味着application-staging.propertiesapplication-test.properties中指定的属性将覆盖application.properties.

\n\n

每个测试类都使用自己的配置文件,因此您需要为每个类指定活动配置文件。

\n\n

您可能感兴趣的另一件事是您可以通过configuration类模拟服务

\n\n
@Configuration\n@Profile("mockEntityService")\npublic class EntityServiceMockProvider {\n\n    @Bean\n    @Primary\n    public EntityService entityService() {\n        EntityService mockedEntityService = Mockito.mock(EntityService.class);\n\n        Entity entity= Mockito.mock(Entity.class);\n        when(mockedEntityService.save(any(Entity.class)))\n                .thenReturn(entity);\n\n        return mockedEntityService ;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在测试类中,您可以使用多个活动配置文件:\ne.g。@ActiveProfiles({"test", "mockEntityService"})

\n\n

因此,EntityService您将使用模拟实现,而不是使用实际的实现。

\n