dasd412 / RemakeDiabetesDiaryAPI

혈당일지 api 리메이크
https://www.diabetes-diary.tk/
1 stars 0 forks source link

how to test JAR file with external secret properties in CI (GitHub Actions) #73

Closed dasd412 closed 2 years ago

dasd412 commented 2 years ago

GitHub Actions log below describes error about external properties. In other words, This error means that spring can't find that properties.

Description:

Parameter 0 of constructor in com.dasd412.remake.api.service.security.EmailService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.

When we code and test in local environment, we don't need to care about this kind of issues.

Why? That's because we locate secret properties ( example : email.properties, database.properties ... etc) on directory classpath:/src/resources/!

dasd412 commented 2 years ago

when CI / CD, we MUST NOT push these properties into GitHub Repository because this is our secret! So, we use .gitignore and make direcory for these secret properties in our AWS servers.

But dillema arises when CI. GitHub actions cannot find these properties!! And we must not push into git repo!!

dasd412 commented 2 years ago

My solution is "make mock properties. and include them in test properties."

For example, I made application-email-mock.properties like below.

spring.mail.host=smtp.test.com
spring.mail.port=1111
spring.mail.username=mock@test.com
spring.mail.password=ThisISmock
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.trust=smtp.test.com

when testing, application usually dosen't use these attributes. So, just write any string.

Then we should include properties in properties for only test.

# inject mockito properties
spring.profiles.include=database-mock,oauth-mock,email-mock

NOTE I wrote @TestPropertySource(locations = "classpath:application-test.properties") annotation in test classes.