|
| 1 | +package com.example.log4u.domain.support.service; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.BDDMockito.*; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.InjectMocks; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | +import org.springframework.data.domain.Page; |
| 15 | +import org.springframework.data.domain.PageImpl; |
| 16 | +import org.springframework.data.domain.PageRequest; |
| 17 | + |
| 18 | +import com.example.log4u.domain.supports.dto.SupportCreateRequestDto; |
| 19 | +import com.example.log4u.domain.supports.dto.SupportGetResponseDto; |
| 20 | +import com.example.log4u.domain.supports.dto.SupportOverviewGetResponseDto; |
| 21 | +import com.example.log4u.domain.supports.entity.Support; |
| 22 | +import com.example.log4u.domain.supports.repository.SupportQuerydsl; |
| 23 | +import com.example.log4u.domain.supports.repository.SupportRepository; |
| 24 | +import com.example.log4u.domain.supports.service.SupportService; |
| 25 | +import com.example.log4u.domain.supports.supportType.SupportType; |
| 26 | + |
| 27 | +@DisplayName("문의 API 단위 테스트") |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +public class SupportServiceTest { |
| 30 | + @InjectMocks |
| 31 | + private SupportService supportService; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private SupportRepository supportRepository; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private SupportQuerydsl supportQuerydsl; |
| 38 | + |
| 39 | + @DisplayName("성공 테스트 : 문의 등록") |
| 40 | + @Test |
| 41 | + void testCreateSupport() { |
| 42 | + long requesterId = 1L; |
| 43 | + SupportCreateRequestDto supportCreateRequestDto = mock(SupportCreateRequestDto.class); |
| 44 | + Support supportEntity = mock(Support.class); |
| 45 | + |
| 46 | + given(supportCreateRequestDto.toEntity(requesterId)).willReturn(supportEntity); |
| 47 | + |
| 48 | + supportService.createSupport(requesterId, supportCreateRequestDto); |
| 49 | + |
| 50 | + verify(supportRepository).save(supportEntity); |
| 51 | + } |
| 52 | + |
| 53 | + @DisplayName("성공 테스트 : 문의 페이지 조회") |
| 54 | + @Test |
| 55 | + void testGetSupportPage() { |
| 56 | + long requesterId = 1L; |
| 57 | + SupportType supportType = SupportType.ETC; |
| 58 | + PageRequest pageable = PageRequest.of(0, 10); |
| 59 | + SupportOverviewGetResponseDto supportOverview = mock(SupportOverviewGetResponseDto.class); |
| 60 | + Page<SupportOverviewGetResponseDto> supportPage = new PageImpl<>(List.of(supportOverview)); |
| 61 | + |
| 62 | + given(supportQuerydsl.getSupportOverviewGetResponseDtoPage(requesterId, pageable, supportType)) |
| 63 | + .willReturn(supportPage); |
| 64 | + |
| 65 | + Page<SupportOverviewGetResponseDto> result = supportService.getSupportPage(requesterId, 1, supportType); |
| 66 | + |
| 67 | + assertNotNull(result); |
| 68 | + assertEquals(1, result.getTotalElements()); |
| 69 | + } |
| 70 | + |
| 71 | + @DisplayName("성공 테스트 : 특정 문의 상세 조회") |
| 72 | + @Test |
| 73 | + void testGetSupportById() { |
| 74 | + long requesterId = 1L; |
| 75 | + Long supportId = 1L; |
| 76 | + SupportGetResponseDto supportGetResponseDto = mock(SupportGetResponseDto.class); |
| 77 | + |
| 78 | + given(supportQuerydsl.getSupportGetResponseDtoById(requesterId, supportId)) |
| 79 | + .willReturn(supportGetResponseDto); |
| 80 | + |
| 81 | + SupportGetResponseDto result = supportService.getSupportById(requesterId, supportId); |
| 82 | + |
| 83 | + assertNotNull(result); |
| 84 | + verify(supportQuerydsl).getSupportGetResponseDtoById(requesterId, supportId); |
| 85 | + } |
| 86 | +} |
0 commit comments