2dor
2dor

Reputation: 1001

@SpringBootTest not using 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:

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

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("test")
@TestPropertySource("classpath:application-test.properties")
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?

Upvotes: 8

Views: 17638

Answers (3)

AKhiL NS
AKhiL NS

Reputation: 11

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

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40078

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:

Upvotes: 0

Kunal Vohra
Kunal Vohra

Reputation: 2846

Change your

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

to

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

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 = "/other-location.properties",
  properties = "kunal.testpropertysource.one=other-property-value")

Upvotes: 2

Related Questions