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/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/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/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/FlowerServiceTest.java b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java new file mode 100644 index 0000000..ad8e879 --- /dev/null +++ b/src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.java @@ -0,0 +1,75 @@ +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 testFindByNameAndFlowerLanguage() { + String name = "장미"; + String flowerLanguage = "사랑"; + Flower flower = Flower.builder() + .name(name) + .period(601L) + .flowerLanguage(flowerLanguage) + .explain("꽃이 아름다워요!") + .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); + } + } +} 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)); + } +} 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); + } +} 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); + } +} 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); + } +} 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); + } +} 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); + } + +} 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); + } +} 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