1+ package dmu .dasom .api .domain .applicant ;
2+
3+ import dmu .dasom .api .domain .applicant .dto .ApplicantCreateRequestDto ;
4+ import dmu .dasom .api .domain .applicant .dto .ApplicantResponseDto ;
5+ import dmu .dasom .api .domain .applicant .entity .Applicant ;
6+ import dmu .dasom .api .domain .applicant .repository .ApplicantRepository ;
7+ import dmu .dasom .api .domain .applicant .service .ApplicantServiceImpl ;
8+ import dmu .dasom .api .domain .common .exception .CustomException ;
9+ import dmu .dasom .api .domain .common .exception .ErrorCode ;
10+ import dmu .dasom .api .global .dto .PageResponse ;
11+ import org .junit .jupiter .api .DisplayName ;
12+ import org .junit .jupiter .api .Test ;
13+ import org .junit .jupiter .api .extension .ExtendWith ;
14+ import org .mockito .InjectMocks ;
15+ import org .mockito .Mock ;
16+ import org .mockito .junit .jupiter .MockitoExtension ;
17+ import org .springframework .data .domain .Page ;
18+ import org .springframework .data .domain .PageImpl ;
19+ import org .springframework .data .domain .PageRequest ;
20+
21+ import java .util .Collections ;
22+
23+ import static org .junit .jupiter .api .Assertions .*;
24+ import static org .mockito .Mockito .*;
25+
26+ @ ExtendWith (MockitoExtension .class )
27+ class ApplicantServiceTest {
28+
29+ @ Mock
30+ private ApplicantRepository applicantRepository ;
31+
32+ @ InjectMocks
33+ private ApplicantServiceImpl applicantService ;
34+
35+ @ Test
36+ @ DisplayName ("지원자 저장 - 성공" )
37+ void apply_success () {
38+ // given
39+ ApplicantCreateRequestDto request = mock (ApplicantCreateRequestDto .class );
40+
41+ // when
42+ applicantService .apply (request );
43+
44+ // then
45+ verify (applicantRepository ).save (request .toEntity ());
46+ }
47+
48+ @ Test
49+ @ DisplayName ("지원자 저장 - 실패" )
50+ void apply_fail () {
51+ }
52+
53+ @ Test
54+ @ DisplayName ("지원자 조회 - 성공" )
55+ void getApplicants_success () {
56+ // given
57+ int page = 0 ;
58+ PageRequest pageRequest = PageRequest .of (page , 20 );
59+ Applicant applicant = mock (Applicant .class );
60+ Page <Applicant > applicants = new PageImpl <>(Collections .singletonList (applicant ), pageRequest , 1 );
61+ when (applicantRepository .findAllWithPageRequest (pageRequest )).thenReturn (applicants );
62+ when (applicant .toApplicantResponse ()).thenReturn (mock (ApplicantResponseDto .class ));
63+
64+ // when
65+ PageResponse <ApplicantResponseDto > response = applicantService .getApplicants (page );
66+
67+ // then
68+ assertNotNull (response );
69+ assertEquals (1 , response .getContent ().size ());
70+ verify (applicantRepository ).findAllWithPageRequest (pageRequest );
71+ }
72+
73+ @ Test
74+ @ DisplayName ("지원자 조회 - 실패 (결과 없음)" )
75+ void getApplicants_fail_emptyResult () {
76+ // given
77+ int page = 0 ;
78+ PageRequest pageRequest = PageRequest .of (page , 20 );
79+ Page <Applicant > applicants = new PageImpl <>(Collections .emptyList (), pageRequest , 0 );
80+ when (applicantRepository .findAllWithPageRequest (pageRequest )).thenReturn (applicants );
81+
82+ // when
83+ CustomException exception = assertThrows (CustomException .class , () -> {
84+ applicantService .getApplicants (page );
85+ });
86+
87+ // then
88+ assertEquals (ErrorCode .EMPTY_RESULT , exception .getErrorCode ());
89+ verify (applicantRepository ).findAllWithPageRequest (pageRequest );
90+ }
91+ }
0 commit comments