@SpringBootTest未使用测试属性

7

我正在使用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
    }
}

正如你所看到的,我在我的Spring Boot测试中使用JUnit5。我尝试了多种编写自定义测试属性的方法,但是我得到了相同的异常:

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

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


你为什么要添加@TestPropertySource注解?它应该读取测试属性文件,并设置活动配置文件。 - daniu
@daniu 理论上你是对的,但如果我不设置文件,则无法看到 jasypt 密码属性。 - 2dor
1
只需将文件移动到 src/test/resources,它就可以运行了 @TudorGrigoriu - Ryuzaki L
3个回答

1
在测试包的资源目录中添加一个application.properties文件。
将资源目录标记为Resource root
在测试主类中添加注释@ExtendWith(SpringExtension.class)

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")

0

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

覆盖属性文件

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

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

Spring配置文件

在本节中,我们将学习如何使用Spring配置文件来解决问题。与先前的方法不同,此方法会合并默认文件和配置文件中的属性。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接