Skip to content

Commit 30c0c11

Browse files
committed
test: MemberService 단위 테스트 구현
1 parent 83e8240 commit 30c0c11

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package dmu.dasom.api.domain.member;
2+
3+
import dmu.dasom.api.domain.common.exception.CustomException;
4+
import dmu.dasom.api.domain.common.exception.ErrorCode;
5+
import dmu.dasom.api.domain.member.dto.SignupRequestDto;
6+
import dmu.dasom.api.domain.member.entity.Member;
7+
import dmu.dasom.api.domain.member.repository.MemberRepository;
8+
import dmu.dasom.api.domain.member.service.MemberServiceImpl;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.junit.jupiter.MockitoExtension;
15+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
16+
17+
import java.util.Optional;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
import static org.mockito.Mockito.*;
21+
22+
@ExtendWith(MockitoExtension.class)
23+
class MemberServiceTest {
24+
25+
@Mock
26+
private BCryptPasswordEncoder encoder;
27+
28+
@Mock
29+
MemberRepository memberRepository;
30+
31+
@InjectMocks
32+
private MemberServiceImpl memberService;
33+
34+
@Test
35+
@DisplayName("이메일로 사용자 조회 - 성공")
36+
void getMemberByEmail_success() {
37+
// given
38+
Optional<Member> member = Optional.ofNullable(mock(Member.class));
39+
String email = "[email protected]";
40+
when(memberRepository.findByEmail(email)).thenReturn(member);
41+
42+
// when
43+
Member memberByEmail = memberService.getMemberByEmail(email);
44+
45+
// then
46+
assertNotNull(memberByEmail);
47+
verify(memberRepository, times(1)).findByEmail(email);
48+
}
49+
50+
@Test
51+
@DisplayName("이메일로 사용자 조회 - 실패")
52+
void getMemberByEmail_fail() {
53+
// given
54+
String email = "[email protected]";
55+
when(memberRepository.findByEmail(email)).thenReturn(Optional.empty());
56+
57+
// when
58+
CustomException exception = assertThrows(CustomException.class, () -> {
59+
memberService.getMemberByEmail(email);
60+
});
61+
62+
// then
63+
assertEquals(ErrorCode.MEMBER_NOT_FOUND, exception.getErrorCode());
64+
verify(memberRepository, times(1)).findByEmail(email);
65+
}
66+
67+
@Test
68+
@DisplayName("이메일 확인 - 존재")
69+
void checkByEmail_true() {
70+
// given
71+
String email = "[email protected]";
72+
when(memberRepository.existsByEmail(email)).thenReturn(true);
73+
74+
// when
75+
boolean result = memberService.checkByEmail(email);
76+
77+
// then
78+
assertTrue(result);
79+
}
80+
81+
@Test
82+
@DisplayName("이메일 확인 - 미존재")
83+
void checkByEmail_false() {
84+
// given
85+
String email = "[email protected]";
86+
when(memberRepository.existsByEmail(email)).thenReturn(false);
87+
88+
// when
89+
boolean result = memberService.checkByEmail(email);
90+
91+
// then
92+
assertFalse(result);
93+
}
94+
95+
@Test
96+
@DisplayName("회원가입 - 성공")
97+
void signUp_success() {
98+
// given
99+
SignupRequestDto request = mock(SignupRequestDto.class);
100+
when(request.getEmail()).thenReturn("[email protected]");
101+
when(request.getPassword()).thenReturn("password");
102+
when(encoder.encode("password")).thenReturn("encodedPassword");
103+
when(memberRepository.existsByEmail("[email protected]")).thenReturn(false);
104+
105+
// when
106+
memberService.signUp(request);
107+
108+
// then
109+
verify(memberRepository, times(1)).save(any());
110+
}
111+
112+
@Test
113+
@DisplayName("회원가입 - 실패")
114+
void signUp_fail() {
115+
// given
116+
SignupRequestDto request = mock(SignupRequestDto.class);
117+
when(request.getEmail()).thenReturn("[email protected]");
118+
when(memberRepository.existsByEmail("[email protected]")).thenReturn(true);
119+
120+
// when
121+
CustomException exception = assertThrows(CustomException.class, () -> {
122+
memberService.signUp(request);
123+
});
124+
125+
// then
126+
assertEquals(ErrorCode.SIGNUP_FAILED, exception.getErrorCode());
127+
verify(memberRepository, never()).save(any());
128+
}
129+
}

0 commit comments

Comments
 (0)