Java @SpringBootTest未使用测试属性

Java @SpringBootTest未使用测试属性,java,spring,spring-boot,jasypt,junit-jupiter,Java,Spring,Spring Boot,Jasypt,Junit Jupiter,我正在使用jasypt对Spring引导应用程序中的application.properties进行加密。我的目标是更新我的集成测试,以便jasypt与测试加密程序密码一起使用。我的问题是我的测试没有覆盖测试属性 依赖项: <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring

我正在使用jasypt对Spring引导应用程序中的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>
如您所见,我正在使用JUnit5进行Spring引导测试。我尝试了多种使用自定义测试属性编写此测试的方法,但我得到了相同的异常:

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

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

改变你的想法

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

在测试类中使用
@RunWith(SpringRunner.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")

有两种方法可以覆盖测试的原始
属性
文件,但无论哪种方法,都必须将其置于
src/test/resources
目录下

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

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

在本节中,我们将学习如何使用Spring概要文件处理我们的问题。与前面的方法不同,此方法合并了默认文件和配置文件中的属性

首先,让我们在src/test/resources:


为什么还要添加
@TestPropertySource
注释?它应该按原样读取活动配置文件设置的测试属性文件。@daniu理论上你是对的,但是如果我没有设置文件不可见,并且没有设置jasypt password属性,只需将文件移动到
src/test/resources
即可@tudorgorigu
# DefaultTest.properties
kunal.testpropertysource.one=default-value
@TestPropertySource(locations = "/other-location.properties",
  properties = "kunal.testpropertysource.one=other-property-value")