|
| 1 | +package com.example.fiurinee.integration.preferlist; |
| 2 | + |
| 3 | +import com.example.fiurinee.domain.member.entity.Member; |
| 4 | +import com.example.fiurinee.domain.preferList.entity.PreferList; |
| 5 | +import com.example.fiurinee.domain.preferList.repository.PreferListRepository; |
| 6 | +import com.example.fiurinee.domain.preferList.service.PreferListService; |
| 7 | +import com.example.fiurinee.global.exception.CustomException; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.mockito.InjectMocks; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.MockitoAnnotations; |
| 13 | + |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | +import static org.mockito.Mockito.*; |
| 19 | + |
| 20 | +public class PreferlistServiceTest { |
| 21 | + @Mock |
| 22 | + private PreferListRepository preferListRepository; |
| 23 | + |
| 24 | + @InjectMocks |
| 25 | + private PreferListService preferListService; |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void setUp() { |
| 29 | + MockitoAnnotations.openMocks(this); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testSave() { |
| 34 | + // Given |
| 35 | + PreferList preferList = PreferList.builder() |
| 36 | + .build(); |
| 37 | + |
| 38 | + // When |
| 39 | + preferListService.save(preferList); |
| 40 | + |
| 41 | + // Then |
| 42 | + verify(preferListRepository, times(1)).save(preferList); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testFindByMemberAndOrder() { |
| 47 | + // Given |
| 48 | + Member member = Member.builder() |
| 49 | + .build(); |
| 50 | + Long order = 1L; |
| 51 | + |
| 52 | + PreferList preferList = PreferList.builder() |
| 53 | + .member(member) |
| 54 | + .preferOrder(order) |
| 55 | + .build(); |
| 56 | + |
| 57 | + when(preferListRepository.findByMemberAndPreferOrder(member, order)).thenReturn(Optional.of(preferList)); |
| 58 | + |
| 59 | + // When |
| 60 | + PreferList foundPreferList = preferListService.findByMemberAndOrder(member, order); |
| 61 | + |
| 62 | + // Then |
| 63 | + assertThat(foundPreferList).isNotNull(); |
| 64 | + assertThat(foundPreferList).isEqualTo(preferList); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testFindByMemberAndOrderThrowsException() { |
| 69 | + // Given |
| 70 | + Member member = Member.builder() |
| 71 | + .build(); |
| 72 | + Long order = 1L; |
| 73 | + |
| 74 | + when(preferListRepository.findByMemberAndPreferOrder(member, order)).thenReturn(Optional.empty()); |
| 75 | + |
| 76 | + // When & Then |
| 77 | + assertThrows(CustomException.class, () -> preferListService.findByMemberAndOrder(member, order)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testDelete() { |
| 82 | + // Given |
| 83 | + PreferList preferList = PreferList.builder() |
| 84 | + .build(); |
| 85 | + |
| 86 | + // When |
| 87 | + preferListService.delete(preferList); |
| 88 | + |
| 89 | + // Then |
| 90 | + verify(preferListRepository, times(1)).delete(preferList); |
| 91 | + } |
| 92 | +} |
0 commit comments