1919import org .springframework .data .domain .PageRequest ;
2020
2121import java .util .Collections ;
22+ import java .util .Optional ;
2223
2324import static org .junit .jupiter .api .Assertions .*;
2425import static org .mockito .Mockito .*;
@@ -37,6 +38,8 @@ class ApplicantServiceTest {
3738 void apply_success () {
3839 // given
3940 ApplicantCreateRequestDto request = mock (ApplicantCreateRequestDto .class );
41+ when (request .getStudentNo ()).thenReturn ("20210000" );
42+ when (applicantRepository .findByStudentNo ("20210000" )).thenReturn (Optional .empty ());
4043
4144 // when
4245 applicantService .apply (request );
@@ -48,6 +51,38 @@ void apply_success() {
4851 @ Test
4952 @ DisplayName ("지원자 저장 - 실패" )
5053 void apply_fail () {
54+ // given
55+ ApplicantCreateRequestDto request = mock (ApplicantCreateRequestDto .class );
56+ when (request .getStudentNo ()).thenReturn ("20210000" );
57+ when (applicantRepository .findByStudentNo ("20210000" )).thenReturn (Optional .of (mock (Applicant .class )));
58+ when (request .getIsOverwriteConfirmed ()).thenReturn (false );
59+
60+ // when
61+ CustomException exception = assertThrows (CustomException .class , () -> {
62+ applicantService .apply (request );
63+ });
64+
65+ // then
66+ verify (applicantRepository ).findByStudentNo ("20210000" );
67+ assertEquals (ErrorCode .DUPLICATED_STUDENT_NO , exception .getErrorCode ());
68+ }
69+
70+ @ Test
71+ @ DisplayName ("지원자 저장 - 덮어쓰기" )
72+ void apply_overwrite () {
73+ // given
74+ ApplicantCreateRequestDto request = mock (ApplicantCreateRequestDto .class );
75+ when (request .getStudentNo ()).thenReturn ("20210000" );
76+ Applicant applicant = mock (Applicant .class );
77+ when (applicantRepository .findByStudentNo ("20210000" )).thenReturn (Optional .of (applicant ));
78+ when (request .getIsOverwriteConfirmed ()).thenReturn (true );
79+
80+ // when
81+ applicantService .apply (request );
82+
83+ // then
84+ verify (applicantRepository ).findByStudentNo ("20210000" );
85+ verify (applicant ).overwrite (request );
5186 }
5287
5388 @ Test
0 commit comments