@SpringBootTest 不使用测试属性

Tud*_*riu 5 java spring jasypt spring-boot junit-jupiter

我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

我有两个 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
    }
}

Run Code Online (Sandbox Code Playgroud)

如您所见,我使用 JUnit5 进行 Spring Boot 测试。我尝试了多种使用自定义测试属性编写此测试的方法,但出现相同的异常:

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

Run Code Online (Sandbox Code Playgroud)

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

Kun*_*hra 2

改变你的

@TestPropertySource("classpath:application-test.properties")
Run Code Online (Sandbox Code Playgroud)

@TestPropertySource(locations="classpath:application-test.properties")
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

让我们将其添加到我们的资源文件夹中:

# DefaultTest.properties
kunal.testpropertysource.one=default-value
Run Code Online (Sandbox Code Playgroud)

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

@TestPropertySource(locations = "/other-location.properties",
  properties = "kunal.testpropertysource.one=other-property-value")
Run Code Online (Sandbox Code Playgroud)