'Jasypt not decrypting properties during junit testing, but works fine when spring boot app runs

I have a Spring Boot application with a few properties encrypted with Jasypt, and I provide the secret key with the following VM option: -Djasypt.encryptor.password=secretkey

Everything is working perfectly when I run my application locally and in production. The issue is, I'm not sure what I need to do to decrypt the properties I have encrypted in my application-test.properties file I am using during jUnit tests. I'm even passing the same VM option above to my jUnit test.

I'm not receiving any sort of exception during jUnit testing that is telling me why I'm unable to decrypt, but the properties I need decrypted just simply are not being decrypted. Is this even possible or is there some sort of configuration I can setup my tests with to utilize Jasypt decrypting.

I'll include the code I have below. The test I am running in particular is for a JavaMailService method. I have the password encrypted in application-test.properties, and the test fails because it cannot authenticate the user (because password is still encrypted)

application-test.properties:

spring.mail.password=ENC(1NrovUC7+YAzVNhbbpHkRi9TghEPys0aQk1nXonk354=)

Main Test Class:

@RunWith(SpringRunner.class)
@WebMvcTest(value = { ApiController.class, RestController.class })
@ContextConfiguration(classes = { MyApplication.class, EmailService.class, EmailServiceTestConfig.class })
@ActiveProfiles("test")
class MyApplicationTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testSendEmail() throws Exception {
        ...
    }

}

EmailServiceTestConfig Class:

@SpringBootConfiguration
public class EmailServiceTestConfig {

    @Value("${spring.mail.username}")
    private String username;

    @Value("${spring.mail.password}")
    private String password;

    @Bean
    public JavaMailSender javaMailService() {
        ...
    }

    private Properties getMailProperties() {
        ...
    }
}

I left out some unimportant information, however from this EmailServiceTestConfig class, I've checked the password retrieved from the properties file, and It has not been decrypted. Any ideas, please let me know! If I find a solution, I'll post here as well. Any additional information you might need, please let me know!



Solution 1:[1]

I had the same issue, I annotated the Test class with @Import(EnableEncryptablePropertiesConfiguration.class).

Yes it was working fine with @SpringBootTest, but it was taking so much time to load the entire application.

Solution 2:[2]

Solved this. I needed to annotate my application's test class with @SpringBootTest. Once I did that, the password became decrypted. Not 100% sure how or why, but I believe that the SpringBootTest annotation was needed to connect Jasypt to the Spring Boot environment during the running of the tests.

Solution 3:[3]

I had a similar Problem with @SpringBootTest annotated tests. When the application starts by the test, it tries to decrypt the passwords from the properties-files. Since I dont want to either put the secret into a application-test.properties file or run the tests everytime with the secret by hand, the application Context couldnt be loaded and the tests failed. But I found the following workaround.

jasypt.encryptor.property.prefix=[[[[[
jasypt.encryptor.property.suffix=]]]]]

I put those into my application-test.properties. This way jasypt doesn't see my passwords in the properties as encrypted, since i overwrite the standard ENC( prefix and ) suffix. Now all my tests run. And since I don't need the decrypted passwords in my tests, all works out.

It doesn't matter, what you enter as prefix or suffix, it just mustn't be likely to be the start of a propertie in your files, so jasypt tries to decrypt something else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mate Mrše
Solution 2 cldaly
Solution 3 Leuchtfeuer