Skip to content

Commit c10f80b

Browse files
committed
refactor: 이메일의 기수 항목을 하드코딩이 아닌 GenerationService 의 값을 읽어서 적용 하도록 수정 및 테스트 코드에 반영 (DASOMBE-16)
1 parent bd036b5 commit c10f80b

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/main/java/dmu/dasom/api/domain/email/service/EmailService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dmu.dasom.api.domain.common.exception.CustomException;
44
import dmu.dasom.api.domain.common.exception.ErrorCode;
55
import dmu.dasom.api.domain.email.enums.MailType;
6+
import dmu.dasom.api.global.generation.service.GenerationService;
67
import jakarta.mail.MessagingException;
78
import jakarta.mail.internet.MimeMessage;
89
import lombok.RequiredArgsConstructor;
@@ -19,6 +20,7 @@ public class EmailService {
1920

2021
private final TemplateEngine templateEngine;
2122
private final JavaMailSender javaMailSender;
23+
private final GenerationService generationService;
2224

2325
@Value("${spring.mail.username}")
2426
private String from;
@@ -28,6 +30,8 @@ public void sendEmail(String to, String name, MailType mailType) throws Messagin
2830
throw new CustomException(ErrorCode.MAIL_TYPE_NOT_VALID);
2931
}
3032

33+
String currentGeneration = generationService.getCurrentGeneration();
34+
3135
// 메일 제목 및 템플릿 설정
3236
String subject;
3337
String emailContent;
@@ -36,8 +40,8 @@ public void sendEmail(String to, String name, MailType mailType) throws Messagin
3640

3741
switch (mailType) {
3842
case DOCUMENT_RESULT -> {
39-
subject = "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM 서류 결과 안내";
40-
emailContent = "먼저 다솜 34기에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
43+
subject = "동양미래대학교 컴퓨터소프트웨어공학과 전공 동아리 DASOM " + currentGeneration +" 서류 결과 안내";
44+
emailContent = "먼저 다솜 " + currentGeneration +"에 많은 관심을 두고 지원해 주셔서 감사드리며,<br>" +
4145
"내부 서류 평가 결과 및 추후 일정에 관해 안내해드리고자 이메일을 발송하게 되었습니다.<br>" +
4246
"서류 전형 결과는 아래 버튼 혹은 홈페이지를 통해 확인이 가능합니다.";
4347
buttonText = "서류 결과 확인하기";
@@ -58,6 +62,7 @@ public void sendEmail(String to, String name, MailType mailType) throws Messagin
5862
context.setVariable("emailContent", emailContent); // 이메일 내용 전달
5963
context.setVariable("buttonUrl", buttonUrl); // 버튼 링크 전달
6064
context.setVariable("buttonText", buttonText);
65+
context.setVariable("generation", currentGeneration);//기수 정보 전달
6166

6267
// HTML 템플릿 처리
6368
String htmlBody = templateEngine.process("email-template", context);

src/test/java/dmu/dasom/api/domain/email/EmailServiceTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dmu.dasom.api.domain.common.exception.ErrorCode;
55
import dmu.dasom.api.domain.email.enums.MailType;
66
import dmu.dasom.api.domain.email.service.EmailService;
7+
import dmu.dasom.api.global.generation.service.GenerationService;
78
import jakarta.mail.MessagingException;
89
import jakarta.mail.internet.MimeMessage;
910
import org.junit.jupiter.api.BeforeEach;
@@ -29,6 +30,9 @@ class EmailServiceTest {
2930
@Mock
3031
private TemplateEngine templateEngine;
3132

33+
@Mock
34+
private GenerationService generationService;
35+
3236
@InjectMocks
3337
private EmailService emailService;
3438

@@ -42,6 +46,9 @@ void setUp() {
4246

4347
// 테스트 환경에서 from 값을 설정
4448
ReflectionTestUtils.setField(emailService, "from", "[email protected]");
49+
50+
// generationService Mock 반환값 설정
51+
when(generationService.getCurrentGeneration()).thenReturn("34기");
4552
}
4653

4754
@Test
@@ -52,9 +59,8 @@ void sendEmail_documentResult() throws MessagingException {
5259
String name = "지원자";
5360
MailType mailType = MailType.DOCUMENT_RESULT;
5461

55-
String expectedTemplate = "email-template";
56-
String expectedHtmlBody = "<html><body>Document Pass</body></html>";
57-
when(templateEngine.process(eq(expectedTemplate), any(Context.class))).thenReturn(expectedHtmlBody);
62+
when(templateEngine.process(eq("email-template"), any(Context.class)))
63+
.thenReturn("<html><body>Document Pass - 34기</body></html>");
5864

5965
// when
6066
emailService.sendEmail(to, name, mailType);
@@ -65,7 +71,8 @@ void sendEmail_documentResult() throws MessagingException {
6571

6672
MimeMessage sentMessage = messageCaptor.getValue();
6773
assertNotNull(sentMessage);
68-
verify(templateEngine).process(eq(expectedTemplate), any(Context.class));
74+
verify(templateEngine).process(eq("email-template"), any(Context.class));
75+
verify(generationService).getCurrentGeneration();
6976
}
7077

7178
@Test
@@ -76,9 +83,8 @@ void sendEmail_finalResult() throws MessagingException {
7683
String name = "지원자";
7784
MailType mailType = MailType.FINAL_RESULT;
7885

79-
String expectedTemplate = "email-template";
80-
String expectedHtmlBody = "<html><body>Final Pass</body></html>";
81-
when(templateEngine.process(eq(expectedTemplate), any(Context.class))).thenReturn(expectedHtmlBody);
86+
when(templateEngine.process(eq("email-template"), any(Context.class)))
87+
.thenReturn("<html><body>Final Pass - 36기</body></html>");
8288

8389
// when
8490
emailService.sendEmail(to, name, mailType);
@@ -89,7 +95,8 @@ void sendEmail_finalResult() throws MessagingException {
8995

9096
MimeMessage sentMessage = messageCaptor.getValue();
9197
assertNotNull(sentMessage);
92-
verify(templateEngine).process(eq(expectedTemplate), any(Context.class));
98+
verify(templateEngine).process(eq("email-template"), any(Context.class));
99+
verify(generationService).getCurrentGeneration();
93100
}
94101

95102
@Test

0 commit comments

Comments
 (0)