Skip to content

Commit 7a38b2f

Browse files
committed
test: SupportService 단위 테스트 작성
1 parent d88007f commit 7a38b2f

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/main/java/com/example/log4u/domain/supports/repository/SupportQuerydsl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Page<SupportOverviewGetResponseDto> getSupportOverviewGetResponseDtoPage(
4343
support.supportType,
4444
support.title,
4545
support.createdAt,
46-
support.answeredAt.isNotNull() // answered 필드는 answeredAt이 null 이 아니면 true
46+
support.updatedAt.isNotNull() // answered 필드는 answeredAt이 null 이 아니면 true
4747
))
4848
.where(builder)
4949
.orderBy(support.createdAt.desc())
@@ -67,7 +67,7 @@ public SupportGetResponseDto getSupportGetResponseDtoById(
6767
support.content,
6868
support.createdAt,
6969
support.answerContent,
70-
support.answeredAt))
70+
support.updatedAt))
7171
.where(support.id.eq(supportId)
7272
.and(support.requesterId.eq(requesterId)))
7373
.fetchOne();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)