2
votes

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!

2
Haven't used jasypt, but looks like you need to activate jasypt beans in your application. Although this jasypt+spring2 is about Spring 2, you might find some useful information on how to activate those in your application. - Dhiraj

2 Answers

0
votes

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.

0
votes

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.