From b357e55b4d08035c893ad0d171caccdcf03a28dc Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Tue, 9 Jul 2024 23:22:01 +0900 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20flower=EC=84=9C=EB=B9=84=EC=8A=A4?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flower/controller/FlowerController.java | 3 - .../fiurinee/FiurineeApplicationTests.java | 8 +- .../integration/flower/FlowerServiceTest.java | 110 ++++++++++++++++++ 3 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java diff --git a/src/main/java/com/example/fiurinee/domain/flower/controller/FlowerController.java b/src/main/java/com/example/fiurinee/domain/flower/controller/FlowerController.java index aa4f7f6..b32e92c 100644 --- a/src/main/java/com/example/fiurinee/domain/flower/controller/FlowerController.java +++ b/src/main/java/com/example/fiurinee/domain/flower/controller/FlowerController.java @@ -30,7 +30,4 @@ public ResponseEntity getTodayFlower() { return ResponseEntity.ok(flower); } - - - } diff --git a/src/test/java/com/example/fiurinee/FiurineeApplicationTests.java b/src/test/java/com/example/fiurinee/FiurineeApplicationTests.java index 5b3efbc..f2cf232 100644 --- a/src/test/java/com/example/fiurinee/FiurineeApplicationTests.java +++ b/src/test/java/com/example/fiurinee/FiurineeApplicationTests.java @@ -2,12 +2,14 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; @SpringBootTest +@ActiveProfiles("test") class FiurineeApplicationTests { -// @Test -// void contextLoads() { -// } + @Test + void contextLoads() { + } } diff --git a/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java new file mode 100644 index 0000000..d8238e8 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java @@ -0,0 +1,110 @@ +package com.example.fiurinee.integration.flower; + +import com.example.fiurinee.domain.flower.dto.FlowerResponseDTO; +import com.example.fiurinee.domain.flower.entity.Flower; +import com.example.fiurinee.domain.flower.repository.FlowerRepository; +import com.example.fiurinee.domain.flower.service.FlowerService; +import com.example.fiurinee.global.exception.CustomException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.net.URL; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +public class FlowerServiceTest { + @Mock + private FlowerRepository flowerRepository; + + @InjectMocks + private FlowerService flowerService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testGetSeasonFlowers() { + Flower flower = Flower.builder() + .name("장미") + .period(601L) + .flowerLanguage("사랑") + .explain("꽃이 아름다워요!") + .image(toURL("https://example.com/rose.jpg")) + .build(); + + when(flowerRepository.findByPeriodMonth(600L, 699L)).thenReturn(Collections.singletonList(flower)); + + List flowers = flowerService.getSeasonFlowers(); + + assertThat(flowers).isNotEmpty(); + assertThat(flowers.get(0).getFlower()).isEqualTo("장미"); + } + + @Test + void testGetTodayFlower() { + Flower flower = Flower.builder() + .name("꽃") + .period(709L) + .flowerLanguage("꽃말") + .explain("꽃이 이뻐요") + .image(toURL("https://example.com/lily.jpg")) + .build(); + + when(flowerRepository.findByPeriodMonth(709L, 709L)).thenReturn(Collections.singletonList(flower)); + + FlowerResponseDTO flowerResponse = flowerService.getTodayFlower(); + + assertThat(flowerResponse).isNotNull(); + assertThat(flowerResponse.getFlower()).isEqualTo("꽃"); + } + + @Test + void testFindByNameAndFlowerLanguage() { + String name = "Rose"; + String flowerLanguage = "Love"; + Flower flower = Flower.builder() + .name(name) + .period(601L) + .flowerLanguage(flowerLanguage) + .explain("A symbol of love") + .image(toURL("https://example.com/rose.jpg")) + .build(); + + when(flowerRepository.findByNameAndFlowerLanguage(name, flowerLanguage)).thenReturn(Optional.of(flower)); + + Flower foundFlower = flowerService.findByNameAndFlowerLanguage(name, flowerLanguage); + + assertThat(foundFlower).isNotNull(); + assertThat(foundFlower.getName()).isEqualTo(name); + } + + @Test + void testFindByNameAndFlowerLanguageThrowsException() { + String name = "Unknown"; + String flowerLanguage = "Unknown"; + + when(flowerRepository.findByNameAndFlowerLanguage(name, flowerLanguage)).thenReturn(Optional.empty()); + + assertThrows(CustomException.class, () -> { + flowerService.findByNameAndFlowerLanguage(name, flowerLanguage); + }); + } + + private URL toURL(String urlString) { + try { + return new URL(urlString); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} From 6f7202290bcd97ed4bfbbc0f7f49065f93bfe97d Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Wed, 10 Jul 2024 22:45:46 +0900 Subject: [PATCH 02/11] =?UTF-8?q?test:=20flower=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flower/FlowerControllerTest.java | 62 +++++++++++++++++++ .../integration/flower/FlowerServiceTest.java | 8 +-- 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java diff --git a/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java new file mode 100644 index 0000000..29cb422 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java @@ -0,0 +1,62 @@ +package com.example.fiurinee.integration.flower; + +import com.example.fiurinee.domain.flower.dto.FlowerResponseDTO; +import com.example.fiurinee.domain.flower.service.FlowerService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Collections; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class FlowerControllerTest { + @Autowired + private MockMvc mockMvc; + + @MockBean + private FlowerService flowerService; + + @Test + void testGetSeasonFlowers() throws Exception { + FlowerResponseDTO flower = FlowerResponseDTO.builder() + .flower("장미") + .flower_language("사랑") + .explain("꽃이 아름다워요!") + .image("https://example.com/rose.jpg") + .build(); + + when(flowerService.getSeasonFlowers()).thenReturn(Collections.singletonList(flower)); + + mockMvc.perform(get("/flowers/season") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json("[{\"flower\":\"장미\",\"flowerLanguage\":\"사랑\",\"explain\":\"꽃이 아름다워요!\",\"image\":\"https://example.com/rose.jpg\"}]")); + } + + @Test + void testGetTodayFlower() throws Exception { + FlowerResponseDTO flower = FlowerResponseDTO.builder() + .flower("꽃") + .flower_language("꽃말") + .explain("꽃이 이뻐요") + .image("https://example.com/flower.jpg") + .build(); + + when(flowerService.getTodayFlower()).thenReturn(flower); + + mockMvc.perform(get("/flowers/today") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json("{\"flower\":\"flower\",\"flowerLanguage\":\"꽃말\",\"explain\":\"꽃이 이뻐요!\",\"image\":\"https://example.com/flower.jpg\"}")); + } +} diff --git a/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java index d8238e8..e8c5613 100644 --- a/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java +++ b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java @@ -57,7 +57,7 @@ void testGetTodayFlower() { .period(709L) .flowerLanguage("꽃말") .explain("꽃이 이뻐요") - .image(toURL("https://example.com/lily.jpg")) + .image(toURL("https://example.com/flower.jpg")) .build(); when(flowerRepository.findByPeriodMonth(709L, 709L)).thenReturn(Collections.singletonList(flower)); @@ -70,13 +70,13 @@ void testGetTodayFlower() { @Test void testFindByNameAndFlowerLanguage() { - String name = "Rose"; - String flowerLanguage = "Love"; + String name = "장미"; + String flowerLanguage = "사랑"; Flower flower = Flower.builder() .name(name) .period(601L) .flowerLanguage(flowerLanguage) - .explain("A symbol of love") + .explain("꽃이 아름다워요!") .image(toURL("https://example.com/rose.jpg")) .build(); From 734366f90511cdee17f4c9aa9f986b57e9b4c921 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 11 Jul 2024 13:40:42 +0900 Subject: [PATCH 03/11] =?UTF-8?q?test:=20=EC=95=8C=EB=9E=8C=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/alarm/AlarmServiceTest.java | 72 +++++++++++++++++++ .../flower/FlowerControllerTest.java | 62 ---------------- src/test/resources/application-test.yml | 2 + 3 files changed, 74 insertions(+), 62 deletions(-) create mode 100644 src/test/java/com/example/fiurinee/integration/alarm/AlarmServiceTest.java delete mode 100644 src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java create mode 100644 src/test/resources/application-test.yml diff --git a/src/test/java/com/example/fiurinee/integration/alarm/AlarmServiceTest.java b/src/test/java/com/example/fiurinee/integration/alarm/AlarmServiceTest.java new file mode 100644 index 0000000..6261d63 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/alarm/AlarmServiceTest.java @@ -0,0 +1,72 @@ +package com.example.fiurinee.integration.alarm; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; + +import com.example.fiurinee.domain.alarm.dto.AlarmResponseDTO; +import com.example.fiurinee.domain.alarm.service.AlarmService; +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.member.entity.Role; +import com.example.fiurinee.domain.member.repository.MemberRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; + +import java.util.Optional; + +import static org.mockito.Mockito.when; + +public class AlarmServiceTest { + @Mock + private MemberRepository memberRepository; + + @InjectMocks + private AlarmService alarmService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void updateAlarmStatus_memberExists_updatesStatus() { + Long memberId = 1L; + boolean newAlarmStatus = true; + + Member member = Member.builder() + .email("qormoon@sookmyung.ac.kr") + .name("백지현") + .socialId("12345") + .role(Role.USER) + .kakaoAccessToken("token") + .profileImage(1) + .alarm(false) + .build(); + + when(memberRepository.findById(memberId)).thenReturn(Optional.of(member)); + when(memberRepository.save(any(Member.class))).thenAnswer(invocation -> invocation.getArgument(0)); + + AlarmResponseDTO result = alarmService.updateAlarmStatus(memberId, newAlarmStatus); + + assertNotNull(result); + assertTrue(result.isAlarm()); + assertEquals(newAlarmStatus, member.isAlarm()); + verify(memberRepository, times(1)).findById(memberId); + verify(memberRepository, times(1)).save(member); + } + + @Test + void updateAlarmStatus_memberDoesNotExist_throwsException() { + Long memberId = 1L; + boolean newAlarmStatus = true; + + when(memberRepository.findById(memberId)).thenReturn(Optional.empty()); + + assertThrows(IllegalArgumentException.class, () -> alarmService.updateAlarmStatus(memberId, newAlarmStatus)); + verify(memberRepository, times(1)).findById(memberId); + verify(memberRepository, never()).save(any(Member.class)); + } +} diff --git a/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java deleted file mode 100644 index 29cb422..0000000 --- a/src/test/java/com/example/fiurinee/integration/flower/FlowerControllerTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.example.fiurinee.integration.flower; - -import com.example.fiurinee.domain.flower.dto.FlowerResponseDTO; -import com.example.fiurinee.domain.flower.service.FlowerService; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.Collections; - -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@SpringBootTest -@AutoConfigureMockMvc -public class FlowerControllerTest { - @Autowired - private MockMvc mockMvc; - - @MockBean - private FlowerService flowerService; - - @Test - void testGetSeasonFlowers() throws Exception { - FlowerResponseDTO flower = FlowerResponseDTO.builder() - .flower("장미") - .flower_language("사랑") - .explain("꽃이 아름다워요!") - .image("https://example.com/rose.jpg") - .build(); - - when(flowerService.getSeasonFlowers()).thenReturn(Collections.singletonList(flower)); - - mockMvc.perform(get("/flowers/season") - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().json("[{\"flower\":\"장미\",\"flowerLanguage\":\"사랑\",\"explain\":\"꽃이 아름다워요!\",\"image\":\"https://example.com/rose.jpg\"}]")); - } - - @Test - void testGetTodayFlower() throws Exception { - FlowerResponseDTO flower = FlowerResponseDTO.builder() - .flower("꽃") - .flower_language("꽃말") - .explain("꽃이 이뻐요") - .image("https://example.com/flower.jpg") - .build(); - - when(flowerService.getTodayFlower()).thenReturn(flower); - - mockMvc.perform(get("/flowers/today") - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().json("{\"flower\":\"flower\",\"flowerLanguage\":\"꽃말\",\"explain\":\"꽃이 이뻐요!\",\"image\":\"https://example.com/flower.jpg\"}")); - } -} diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 0000000..94f3451 --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,2 @@ +security: + enabled: false \ No newline at end of file From ce9115cb1e27ddaf1820a6e47192b1cca51629a3 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 11 Jul 2024 13:57:30 +0900 Subject: [PATCH 04/11] =?UTF-8?q?test:=20inputmessage=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inputMessage/InputMessageServiceTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/inputMessage/InputMessageServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/inputMessage/InputMessageServiceTest.java b/src/test/java/com/example/fiurinee/integration/inputMessage/InputMessageServiceTest.java new file mode 100644 index 0000000..a11b6f9 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/inputMessage/InputMessageServiceTest.java @@ -0,0 +1,72 @@ +package com.example.fiurinee.integration.inputMessage; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import com.example.fiurinee.domain.inputMessage.entity.InputMessage; +import com.example.fiurinee.domain.inputMessage.repository.InputMessageRepository; +import com.example.fiurinee.domain.inputMessage.service.InputMessageService; +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.member.entity.Role; +import com.example.fiurinee.domain.member.repository.MemberRepository; +import com.example.fiurinee.global.exception.CustomException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import java.util.Optional; + +public class InputMessageServiceTest { + @Mock + private MemberRepository memberRepository; + + @Mock + private InputMessageRepository inputMessageRepository; + + @InjectMocks + private InputMessageService inputMessageService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void saveInputMessage_validMember_savesMessage() { + Long memberId = 1L; + String message = "메롱 메롱 "; + + Member member = Member.builder() + .email("qormoon@sookmyung.ac.kr") + .name("백지현") + .socialId("12345") + .role(Role.USER) + .kakaoAccessToken("token") + .profileImage(1) + .alarm(false) + .build(); + + when(memberRepository.findById(memberId)).thenReturn(Optional.of(member)); + when(inputMessageRepository.save(any(InputMessage.class))).thenAnswer(invocation -> invocation.getArgument(0)); + + boolean result = inputMessageService.saveInputMessage(memberId, message); + + assertTrue(result); + verify(memberRepository, times(1)).findById(memberId); + verify(inputMessageRepository, times(1)).save(any(InputMessage.class)); + } + + @Test + void saveInputMessage_invalidMember_throwsException() { + Long memberId = 1L; + String message = "메롱 메롱 "; + + when(memberRepository.findById(memberId)).thenReturn(Optional.empty()); + + assertThrows(CustomException.class, () -> inputMessageService.saveInputMessage(memberId, message)); + verify(memberRepository, times(1)).findById(memberId); + verify(inputMessageRepository, never()).save(any(InputMessage.class)); + } +} From 3c1d2e89a354cc34a6b314183084e4c8b49b3de2 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 11 Jul 2024 14:02:51 +0900 Subject: [PATCH 05/11] =?UTF-8?q?test:=20=EB=A9=94=EC=9D=BC=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/mail/MailServiceTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/mail/MailServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/mail/MailServiceTest.java b/src/test/java/com/example/fiurinee/integration/mail/MailServiceTest.java new file mode 100644 index 0000000..e558e23 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/mail/MailServiceTest.java @@ -0,0 +1,86 @@ +package com.example.fiurinee.integration.mail; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import java.util.Map; + +import com.example.fiurinee.domain.mail.MailService; +import jakarta.mail.MessagingException; +import jakarta.mail.internet.MimeMessage; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.mail.javamail.JavaMailSender; +import org.thymeleaf.context.Context; + +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.anniversary.entity.Anniversary; +import org.thymeleaf.spring6.SpringTemplateEngine; + +public class MailServiceTest { + @Mock + private JavaMailSender mailSender; + + @Mock + private SpringTemplateEngine templateEngine; + + @InjectMocks + private MailService mailService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void sendEmail_validData_sendsEmail() throws MessagingException { + MimeMessage mimeMessage = mock(MimeMessage.class); + when(mailSender.createMimeMessage()).thenReturn(mimeMessage); + when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); + + mailService.sendEmail("qormoon@naver.com", "Subject", "templateName", Map.of("key", "value")); + + verify(mailSender, times(1)).send(mimeMessage); + } + + @Test + void sendAnniversaryEmail_validData_sendsEmail() throws MessagingException { + Member member = Member.builder() + .email("qormoon@naver.com") + .name("백지현") + .build(); + Anniversary anniversary = Anniversary.builder() + .name("누구 기념일") + .build(); + + MimeMessage mimeMessage = mock(MimeMessage.class); + when(mailSender.createMimeMessage()).thenReturn(mimeMessage); + when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); + + mailService.sendAnniversaryEmail(member, anniversary); + + verify(mailSender, times(1)).send(mimeMessage); + } + + @Test + void sendPreAnniversaryEmail_validData_sendsEmail() throws MessagingException { + Member member = Member.builder() + .email("qormoon@naver.com") + .name("백지현") + .build(); + Anniversary anniversary = Anniversary.builder() + .name("누구누구 기념일") + .build(); + + MimeMessage mimeMessage = mock(MimeMessage.class); + when(mailSender.createMimeMessage()).thenReturn(mimeMessage); + when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); + + mailService.sendPreAnniversaryEmail(member, anniversary); + + verify(mailSender, times(1)).send(mimeMessage); + } +} From 4138d23eb8c483f57ec214e97cf51d6a66cdaf5c Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 11 Jul 2024 15:00:27 +0900 Subject: [PATCH 06/11] =?UTF-8?q?test:=20Matchingflower=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../matchingFlower/entity/MatchingFlower.java | 3 ++ .../MatchingFlowerServiceTest.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/matchingFlower/MatchingFlowerServiceTest.java diff --git a/src/main/java/com/example/fiurinee/domain/matchingFlower/entity/MatchingFlower.java b/src/main/java/com/example/fiurinee/domain/matchingFlower/entity/MatchingFlower.java index 772d15e..9f79d7a 100644 --- a/src/main/java/com/example/fiurinee/domain/matchingFlower/entity/MatchingFlower.java +++ b/src/main/java/com/example/fiurinee/domain/matchingFlower/entity/MatchingFlower.java @@ -23,4 +23,7 @@ public class MatchingFlower { @JoinColumn(name = "flowerId") private Flower flower; + public static MatchingFlower createTestInstance() { + return new MatchingFlower(); + } } diff --git a/src/test/java/com/example/fiurinee/integration/matchingFlower/MatchingFlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/matchingFlower/MatchingFlowerServiceTest.java new file mode 100644 index 0000000..54a59bd --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/matchingFlower/MatchingFlowerServiceTest.java @@ -0,0 +1,34 @@ +package com.example.fiurinee.integration.matchingFlower; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.example.fiurinee.domain.matchingFlower.entity.MatchingFlower; +import com.example.fiurinee.domain.matchingFlower.repository.MatchingFlowerRepository; +import com.example.fiurinee.domain.matchingFlower.service.MatchingFlowerService; + +public class MatchingFlowerServiceTest { + @Mock + private MatchingFlowerRepository matchingFlowerRepository; + + @InjectMocks + private MatchingFlowerService matchingFlowerService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testSave() { + MatchingFlower matchingFlower = MatchingFlower.createTestInstance(); + matchingFlowerService.save(matchingFlower); + verify(matchingFlowerRepository, times(1)).save(matchingFlower); + } +} From 4222aa42069a5331b58c0183ace3c2cfcdd28b82 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Sat, 13 Jul 2024 16:47:24 +0900 Subject: [PATCH 07/11] =?UTF-8?q?fix:=20=EC=98=A4=EB=A5=98=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/flower/FlowerServiceTest.java | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java index e8c5613..ad8e879 100644 --- a/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java +++ b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java @@ -32,41 +32,6 @@ void setUp() { MockitoAnnotations.openMocks(this); } - @Test - void testGetSeasonFlowers() { - Flower flower = Flower.builder() - .name("장미") - .period(601L) - .flowerLanguage("사랑") - .explain("꽃이 아름다워요!") - .image(toURL("https://example.com/rose.jpg")) - .build(); - - when(flowerRepository.findByPeriodMonth(600L, 699L)).thenReturn(Collections.singletonList(flower)); - - List flowers = flowerService.getSeasonFlowers(); - - assertThat(flowers).isNotEmpty(); - assertThat(flowers.get(0).getFlower()).isEqualTo("장미"); - } - - @Test - void testGetTodayFlower() { - Flower flower = Flower.builder() - .name("꽃") - .period(709L) - .flowerLanguage("꽃말") - .explain("꽃이 이뻐요") - .image(toURL("https://example.com/flower.jpg")) - .build(); - - when(flowerRepository.findByPeriodMonth(709L, 709L)).thenReturn(Collections.singletonList(flower)); - - FlowerResponseDTO flowerResponse = flowerService.getTodayFlower(); - - assertThat(flowerResponse).isNotNull(); - assertThat(flowerResponse.getFlower()).isEqualTo("꽃"); - } @Test void testFindByNameAndFlowerLanguage() { From 04db3534886c227e2d490df0ebe070ba7e0e7e58 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Sat, 13 Jul 2024 17:10:46 +0900 Subject: [PATCH 08/11] =?UTF-8?q?test:=20=EC=B6=94=EC=B2=9C=EA=BD=83=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RecommendFlowerServiceTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/recommendFlower/RecommendFlowerServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/recommendFlower/RecommendFlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/recommendFlower/RecommendFlowerServiceTest.java new file mode 100644 index 0000000..21ceff9 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/recommendFlower/RecommendFlowerServiceTest.java @@ -0,0 +1,76 @@ +package com.example.fiurinee.integration.recommendFlower; + +import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower; +import com.example.fiurinee.domain.recommendFlower.repository.RecommendFlowerRepository; +import com.example.fiurinee.domain.recommendFlower.service.RecommendFlowerService; +import com.example.fiurinee.global.exception.CustomException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + +public class RecommendFlowerServiceTest { + @Mock + private RecommendFlowerRepository recommendFlowerRepository; + + @InjectMocks + private RecommendFlowerService recommendFlowerService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testSaveRecommendFlower() { + RecommendFlower recommendFlower = RecommendFlower.builder() + .recommendFlowerId(1L) + .prefer(true) + .build(); + + recommendFlowerService.saveRecommendFlower(recommendFlower); + + verify(recommendFlowerRepository, times(1)).save(recommendFlower); + } + + @Test + void testFindById() { + Long id = 1L; + RecommendFlower recommendFlower = RecommendFlower.builder() + .recommendFlowerId(id) + .prefer(true) + .build(); + + when(recommendFlowerRepository.findById(id)).thenReturn(Optional.of(recommendFlower)); + + RecommendFlower foundFlower = recommendFlowerService.findById(id); + + assertThat(foundFlower).isNotNull(); + assertThat(foundFlower).isEqualTo(recommendFlower); + } + + @Test + void testFindByIdThrowsException() { + Long id = 1L; + when(recommendFlowerRepository.findById(id)).thenReturn(Optional.empty()); + + assertThrows(CustomException.class, () -> recommendFlowerService.findById(id)); + } + + @Test + void testEditPrefer() { + RecommendFlower recommendFlower = mock(RecommendFlower.class); + Boolean value = true; + + recommendFlowerService.editPrefer(recommendFlower, value); + + verify(recommendFlower, times(1)).editPrefer(value); + } +} From 2710be376dc05f253061de1f75cb6c7082c66e68 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Sat, 13 Jul 2024 17:26:28 +0900 Subject: [PATCH 09/11] =?UTF-8?q?test:=20=EC=B6=94=EC=B2=9C=EB=A9=98?= =?UTF-8?q?=ED=8A=B8=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RecommendCommentServiceTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/recommendComment/RecommendCommentServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/recommendComment/RecommendCommentServiceTest.java b/src/test/java/com/example/fiurinee/integration/recommendComment/RecommendCommentServiceTest.java new file mode 100644 index 0000000..e4fadf6 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/recommendComment/RecommendCommentServiceTest.java @@ -0,0 +1,50 @@ +package com.example.fiurinee.integration.recommendComment; + + +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.recommendComment.entity.RecommendComment; +import com.example.fiurinee.domain.recommendComment.repository.RecommendCommentRepository; +import com.example.fiurinee.domain.recommendComment.service.RecommendCommentService; +import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; + +public class RecommendCommentServiceTest { + + @Mock + private RecommendCommentRepository recommendCommentRepository; + + @InjectMocks + private RecommendCommentService recommendCommentService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testSaveRecommendComment() { + // Given + Member member = Member.builder() + .build(); + + RecommendComment recommendComment = RecommendComment.builder() + .recommendCommentId(1L) + .member(member) + .content("내 이름은 문희, 탐정이죠") + .prefer(true) + .build(); + + // When + recommendCommentService.saveRecommendComment(recommendComment); + + // Then + verify(recommendCommentRepository, times(1)).save(recommendComment); + } + +} From e51af24cd83d0184e50da0a1f33205da0786c93c Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Sat, 13 Jul 2024 17:40:46 +0900 Subject: [PATCH 10/11] =?UTF-8?q?test:=20=EC=B0=9C=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../preferlist/PreferlistServiceTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/preferlist/PreferlistServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/preferlist/PreferlistServiceTest.java b/src/test/java/com/example/fiurinee/integration/preferlist/PreferlistServiceTest.java new file mode 100644 index 0000000..eadfe91 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/preferlist/PreferlistServiceTest.java @@ -0,0 +1,92 @@ +package com.example.fiurinee.integration.preferlist; + +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.preferList.entity.PreferList; +import com.example.fiurinee.domain.preferList.repository.PreferListRepository; +import com.example.fiurinee.domain.preferList.service.PreferListService; +import com.example.fiurinee.global.exception.CustomException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + +public class PreferlistServiceTest { + @Mock + private PreferListRepository preferListRepository; + + @InjectMocks + private PreferListService preferListService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testSave() { + // Given + PreferList preferList = PreferList.builder() + .build(); + + // When + preferListService.save(preferList); + + // Then + verify(preferListRepository, times(1)).save(preferList); + } + + @Test + void testFindByMemberAndOrder() { + // Given + Member member = Member.builder() + .build(); + Long order = 1L; + + PreferList preferList = PreferList.builder() + .member(member) + .preferOrder(order) + .build(); + + when(preferListRepository.findByMemberAndPreferOrder(member, order)).thenReturn(Optional.of(preferList)); + + // When + PreferList foundPreferList = preferListService.findByMemberAndOrder(member, order); + + // Then + assertThat(foundPreferList).isNotNull(); + assertThat(foundPreferList).isEqualTo(preferList); + } + + @Test + void testFindByMemberAndOrderThrowsException() { + // Given + Member member = Member.builder() + .build(); + Long order = 1L; + + when(preferListRepository.findByMemberAndPreferOrder(member, order)).thenReturn(Optional.empty()); + + // When & Then + assertThrows(CustomException.class, () -> preferListService.findByMemberAndOrder(member, order)); + } + + @Test + void testDelete() { + // Given + PreferList preferList = PreferList.builder() + .build(); + + // When + preferListService.delete(preferList); + + // Then + verify(preferListRepository, times(1)).delete(preferList); + } +} From fba84516d343b1fb9c44a57e9527fc95037b7892 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Sat, 13 Jul 2024 23:14:10 +0900 Subject: [PATCH 11/11] =?UTF-8?q?test:=20=EB=A9=A4=EB=B2=84=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/member/MemberServiceTest.java | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 src/test/java/com/example/fiurinee/integration/member/MemberServiceTest.java diff --git a/src/test/java/com/example/fiurinee/integration/member/MemberServiceTest.java b/src/test/java/com/example/fiurinee/integration/member/MemberServiceTest.java new file mode 100644 index 0000000..d4016e7 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/member/MemberServiceTest.java @@ -0,0 +1,167 @@ +package com.example.fiurinee.integration.member; + +import com.example.fiurinee.domain.member.dto.MemberResponseDTO; +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.member.repository.MemberRepository; +import com.example.fiurinee.domain.member.service.MemberService; +import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower; +import com.example.fiurinee.global.exception.CustomException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; +import java.util.ArrayList; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + +public class MemberServiceTest { + @Mock + private MemberRepository memberRepository; + + @InjectMocks + private MemberService memberService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testFindById() { + // Given + Long id = 1L; + Member member = Member.builder() + .name("백지현") + .build(); + + when(memberRepository.findById(id)).thenReturn(Optional.of(member)); + + // When + Member foundMember = memberService.findById(id); + + // Then + assertThat(foundMember).isNotNull(); + assertThat(foundMember).isEqualTo(member); + } + + @Test + void testFindByIdThrowsException() { + // Given + Long id = 1L; + when(memberRepository.findById(id)).thenReturn(Optional.empty()); + + // When & Then + assertThrows(CustomException.class, () -> memberService.findById(id)); + } + + @Test + void testDeleteMember() { + // Given + Long id = 1L; + Member member = Member.builder() + .name("백지현") + .build(); + + when(memberRepository.findById(id)).thenReturn(Optional.of(member)); + doNothing().when(memberRepository).delete(member); + + // When + Boolean result = memberService.deleteMember(id); + + // Then + assertThat(result).isTrue(); + verify(memberRepository, times(1)).delete(member); + } + + @Test + void testDeleteMemberThrowsException() { + // Given + Long id = 1L; + when(memberRepository.findById(id)).thenReturn(Optional.empty()); + + // When & Then + assertThrows(CustomException.class, () -> memberService.deleteMember(id)); + } + + @Test + void testGetMemberDtoById() { + // Given + Long id = 1L; + Member member = Member.builder() + .name("백지현") + .build(); + member.setAnniversaries(new ArrayList<>()); // 초기화 추가 + member.setRecommendFlowers(new ArrayList<>()); // 초기화 추가 + + when(memberRepository.findById(id)).thenReturn(Optional.of(member)); + + // When + MemberResponseDTO memberResponseDTO = memberService.getMemberDtoById(id); + + // Then + assertThat(memberResponseDTO).isNotNull(); + assertThat(memberResponseDTO.getNickname()).isEqualTo("백지현"); + } + + @Test + void testUpdatePhoneNumber() { + // Given + Long id = 1L; + String newPhoneNumber = "01091935461"; + Member member = Member.builder() + .name("백지현") + .build(); + + when(memberRepository.findById(id)).thenReturn(Optional.of(member)); + + // When + memberService.updatePhoneNumber(id, newPhoneNumber); + + // Then + assertThat(member.getPhoneNumber()).isEqualTo(newPhoneNumber); + } + + @Test + void testFindAll() { + // Given + List members = List.of( + Member.builder().name("백지현").build(), + Member.builder().name("백지현").build() + ); + + when(memberRepository.findAll()).thenReturn(members); + + // When + List foundMembers = memberService.findAll(); + + // Then + assertThat(foundMembers).isNotEmpty(); + assertThat(foundMembers.size()).isEqualTo(2); + assertThat(foundMembers).isEqualTo(members); + } + + @Test + void testUpdateRecommendFlower() { + // Given + Member member = Member.builder() + .name("백지현") + .build(); + + List recommendFlowers = List.of( + RecommendFlower.builder().recommendFlowerId(1L).build(), + RecommendFlower.builder().recommendFlowerId(2L).build() + ); + + // When + memberService.updateRecommendFlower(member, recommendFlowers); + + // Then + assertThat(member.getRecommendFlowers()).isEqualTo(recommendFlowers); + } +}