@SpringBootTest不使用测试属性

huangapple go评论92阅读模式
英文:

@SpringBootTest not using test properties

问题

我正在使用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设置为测试密码,并使用测试密码将spring.data.mongodb.uri覆盖为jasypt的ENC值。我的测试如下所示:

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测试中使用了JUnit 5。我尝试过多种编写此测试的方式,使用自定义的测试属性,但我得到了相同的异常:

无法解密:ENC(JsHRQaQN0cuHgrq/0o ...)

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

英文:

I am using jasypt for encrypting application.properties in a Spring Boot application. My goal is to update my integration tests so that jasypt is used with test encryptor password. My problem is that my test is not overriding the test properties.

Dependencies:

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

I have two application.properties, one in src/main/resources/application.properties and another in src/test/application-test.properties. In the test property I am setting jasypt.encryptor.password=test and I override the spring.data.mongodb.uri with the jasypt ENC value using the test password.
My test looks like this:

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(&quot;test&quot;)
@TestPropertySource(&quot;classpath:application-test.properties&quot;)
public class SpringBootAppTest{


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

As you can see, I am using JUnit5 for my Spring Boot test. I've tried multiple ways of writing this test with custom test properties, but I get the same exception:

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

The value it's the spring.data.mongodb.uri property value from src/main/resources/application.properties not the value from application-test.properties. What am I doing wrong and how can I override my "main" properties with the test properties?

答案1

得分: 1

将您的

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

更改为

@TestPropertySource(locations = "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")
英文:

Change your

@TestPropertySource(&quot;classpath:application-test.properties&quot;)

to

@TestPropertySource(locations=&quot;classpath:application-test.properties&quot;)

With @RunWith(SpringRunner.class) at your Test class

If that doesn't work here is the master approach

Use @TestPropertySource at class level. By default, this annotation tries to load a properties file relative to the class that declared the annotation.

In your case, for example, if our test class is in the com.kunal.testpropertysource package, then we'll need the file com/kunal/testpropertysource/DefaultTest.properties in our classpath.

Let's add it to our resources folder then:

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

Additionally, we can change the default configuration file location, or add extra properties that will have even higher precedence:

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

答案2

得分: 1

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

将资源目录标记为 Resource root

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

英文:

Add an application.properties file in the resources directory inside the test package.

mark the resources directory as Resource root.

add the annotation @ExtendWith(SpringExtension.class) in the test main class

答案3

得分: 0

有几种方法可以覆盖原始的 properties 文件以进行测试,但在任何方法中,您都需要将其放置在 src/test/resources 目录下。

覆盖属性文件

>现在,我们将通过将属性文件放入测试资源中来覆盖属性。此文件必须与默认文件位于相同的类路径上。
>
>此外,它应该包含默认文件中指定的所有属性键。因此,我们将把 application.properties 文件添加到 src/test/resources 中:

Spring 配置文件

>在本节中,我们将学习如何使用 Spring 配置文件来处理我们的问题。与先前的方法不同,这种方法会合并默认文件和配置文件中的属性。
>
>首先,让我们在 src/test/resources 中创建一个 application–test.properties 文件:

英文:

There are couple of ways to override the original properties file for test, but in either any approach you have to place it under src/test/resources directory

Overriding a Property File

>Now, we'll override properties by putting the property file in the test resources. This file must be on the same classpath as the default one.
>
>Additionally, it should contain all the property keys specified in the default file. Therefore, we'll add the application.properties file into the src/test/resources:

Spring Profiles

>In this section, we'll learn how to deal with our issue by using Spring Profiles. Unlike the previous method, this one merges properties from the default file and the profiled file.
>
>First, let's create an application–test.properties file in the src/test/resources:

huangapple
  • 本文由 发表于 2020年5月5日 18:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61611197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定