|
| 1 | +package com.example.fiurinee.integration.recommendFlower; |
| 2 | + |
| 3 | +import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower; |
| 4 | +import com.example.fiurinee.domain.recommendFlower.repository.RecommendFlowerRepository; |
| 5 | +import com.example.fiurinee.domain.recommendFlower.service.RecommendFlowerService; |
| 6 | +import com.example.fiurinee.global.exception.CustomException; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.mockito.InjectMocks; |
| 10 | +import org.mockito.Mock; |
| 11 | +import org.mockito.MockitoAnnotations; |
| 12 | + |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +public class RecommendFlowerServiceTest { |
| 20 | + @Mock |
| 21 | + private RecommendFlowerRepository recommendFlowerRepository; |
| 22 | + |
| 23 | + @InjectMocks |
| 24 | + private RecommendFlowerService recommendFlowerService; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void setUp() { |
| 28 | + MockitoAnnotations.openMocks(this); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testSaveRecommendFlower() { |
| 33 | + RecommendFlower recommendFlower = RecommendFlower.builder() |
| 34 | + .recommendFlowerId(1L) |
| 35 | + .prefer(true) |
| 36 | + .build(); |
| 37 | + |
| 38 | + recommendFlowerService.saveRecommendFlower(recommendFlower); |
| 39 | + |
| 40 | + verify(recommendFlowerRepository, times(1)).save(recommendFlower); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testFindById() { |
| 45 | + Long id = 1L; |
| 46 | + RecommendFlower recommendFlower = RecommendFlower.builder() |
| 47 | + .recommendFlowerId(id) |
| 48 | + .prefer(true) |
| 49 | + .build(); |
| 50 | + |
| 51 | + when(recommendFlowerRepository.findById(id)).thenReturn(Optional.of(recommendFlower)); |
| 52 | + |
| 53 | + RecommendFlower foundFlower = recommendFlowerService.findById(id); |
| 54 | + |
| 55 | + assertThat(foundFlower).isNotNull(); |
| 56 | + assertThat(foundFlower).isEqualTo(recommendFlower); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testFindByIdThrowsException() { |
| 61 | + Long id = 1L; |
| 62 | + when(recommendFlowerRepository.findById(id)).thenReturn(Optional.empty()); |
| 63 | + |
| 64 | + assertThrows(CustomException.class, () -> recommendFlowerService.findById(id)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testEditPrefer() { |
| 69 | + RecommendFlower recommendFlower = mock(RecommendFlower.class); |
| 70 | + Boolean value = true; |
| 71 | + |
| 72 | + recommendFlowerService.editPrefer(recommendFlower, value); |
| 73 | + |
| 74 | + verify(recommendFlower, times(1)).editPrefer(value); |
| 75 | + } |
| 76 | +} |
0 commit comments