Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 627
cldaly Avatar asked Oct 23 '25 09:10

cldaly


1 Answers

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.

like image 132
Shino Avatar answered Oct 25 '25 23:10

Shino