Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 970
2dor Avatar asked Nov 07 '22 08:11

2dor


1 Answers

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")
like image 92
Kunal Vohra Avatar answered Nov 14 '22 23:11

Kunal Vohra