【问题标题】:@SpringBootTest not using test properties@SpringBootTest 不使用测试属性
【发布时间】:2020-05-05 10:41:08
【问题描述】:

我正在使用 jasypt 加密 Spring Boot 应用程序中的 application.properties。我的目标是更新我的集成测试,以便将 jasypt 与测试加密器密码一起使用。我的问题是我的测试没有覆盖测试属性。

依赖关系:

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>

我有两个 application.properties,一个在 src/main/resources/application.properties,另一个在 src/test/application-test.properties。在测试属性中,我设置 jasypt.encryptor.password=test 并使用测试密码用 jasypt ENC 值覆盖 spring.data.mongodb.uri。 我的测试如下所示:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;


@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
@TestPropertySource("classpath:application-test.properties")
public class SpringBootAppTest{


    @Test
    public void shouldLoadContext(){
        //nothing to test
    }
}

如您所见,我正在使用 JUnit5 进行 Spring Boot 测试。我尝试了多种使用自定义测试属性编写此测试的方法,但我得到了同样的例外:

Unable to decrypt: ENC(JsHRQaQN0cuHgrq/0o ...)

它的值是来自 src/main/resources/application.properties 的 spring.data.mongodb.uri 属性值,而不是来自 application-test.properties 的值。我做错了什么,如何用测试属性覆盖我的“主要”属性?

【问题讨论】:

  • 为什么还要加上@TestPropertySource注解?它应该按原样读取具有活动配置文件集的测试属性文件。
  • @daniu 理论上你是对的,但如果我不设置文件不可见并且 jasypt 密码属性未设置
  • 只需将文件移动到src/test/resources 即可使用@TudorGrigoriu

标签: java spring spring-boot jasypt junit-jupiter


【解决方案1】:

改变你的

@TestPropertySource("classpath:application-test.properties")

@TestPropertySource(locations="classpath:application-test.properties")

@RunWith(SpringRunner.class) 在你的测试课上

如果这不起作用,那就是主要方法

class 级别使用@TestPropertySource。默认情况下,此注解会尝试加载与声明该注​​解的类相关的属性文件。

在您的情况下,例如,如果我们的测试类在 com.kunal.testpropertysource 包中,那么我们的类路径中需要文件 com/kunal/testpropertysource/DefaultTest.properties。

然后我们将它添加到我们的资源文件夹中:

# DefaultTest.properties
kunal.testpropertysource.one=default-value

此外,我们可以更改默认配置文件位置,或添加具有更高优先级的额外属性:

@TestPropertySource(locations = "/other-location.properties",
  properties = "kunal.testpropertysource.one=other-property-value")

【讨论】:

    【解决方案2】:

    有几种方法可以覆盖原始 properties 文件进行测试,但无论哪种方法,您都必须将其放在 src/test/resources 目录下

    Overriding a Property File

    现在,我们将通过将属性文件放入测试资源中来覆盖属性。此文件必须与默认文件位于同一类路径中。

    此外,它应该包含默认文件中指定的所有属性键。因此,我们将 application.properties 文件添加到 src/test/resources 中:

    Spring Profiles

    在本节中,我们将学习如何使用 Spring Profiles 来处理我们的问题。与前一种方法不同的是,这种方法合并了默认文件和配置文件中的属性。

    首先,让我们在 src/test/resources 中创建一个 application-test.properties 文件:

    【讨论】:

      【解决方案3】:

      在测试包内的资源目录中添加application.properties文件。

      将资源目录标记为Resource root

      在测试主类中添加注解@ExtendWith(SpringExtension.class)

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 1970-01-01
        • 2017-05-28
        • 2018-08-18
        • 2017-07-24
        • 2019-11-07
        • 1970-01-01
        • 2019-06-14
        • 2022-01-25
        相关资源
        最近更新 更多