Skip to content

Commit 609d106

Browse files
committed
Comment: 테스트 코드 주석 추가
1 parent 8e7614d commit 609d106

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/test/java/com/back/domain/user/controller/AuthControllerTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AuthControllerTest {
3535
@Test
3636
@DisplayName("정상 회원가입 → 201 Created")
3737
void register_success() throws Exception {
38-
// given
38+
// given: 정상적인 회원가입 요청 JSON
3939
String body = """
4040
{
4141
"username": "testuser",
@@ -45,32 +45,35 @@ void register_success() throws Exception {
4545
}
4646
""";
4747

48-
// when
48+
// when: 회원가입 API 호출
4949
ResultActions resultActions = mvc.perform(
5050
post("/api/auth/register")
5151
.contentType(MediaType.APPLICATION_JSON)
5252
.content(body)
5353
).andDo(print());
5454

55-
// then
55+
// then: 응답 값과 DB 저장값 검증
5656
resultActions
5757
.andExpect(status().isCreated())
5858
.andExpect(jsonPath("$.success").value(true))
5959
.andExpect(jsonPath("$.data.username").value("testuser"))
6060
.andExpect(jsonPath("$.data.email").value("[email protected]"))
6161
.andExpect(jsonPath("$.data.nickname").value("홍길동"));
6262

63+
// DB에서 저장된 User 상태 검증
6364
User saved = userRepository.findByUsername("testuser").orElseThrow();
6465
assertThat(saved.getUserStatus()).isEqualTo(UserStatus.PENDING);
6566
}
6667

6768
@Test
6869
@DisplayName("중복 username → 409 Conflict")
6970
void register_duplicateUsername() throws Exception {
71+
// given: 이미 존재하는 username을 가진 User 저장
7072
User existing = User.createUser("dupuser", "[email protected]", "password123!");
7173
existing.setUserProfile(new UserProfile(existing, "dupnick", null, null, null, 0));
7274
userRepository.save(existing);
7375

76+
// 동일 username으로 회원가입 요청
7477
String body = """
7578
{
7679
"username": "dupuser",
@@ -80,6 +83,7 @@ void register_duplicateUsername() throws Exception {
8083
}
8184
""";
8285

86+
// when & then: 409 Conflict 응답 및 에러 코드 확인
8387
mvc.perform(post("/api/auth/register")
8488
.contentType(MediaType.APPLICATION_JSON)
8589
.content(body))
@@ -91,10 +95,12 @@ void register_duplicateUsername() throws Exception {
9195
@Test
9296
@DisplayName("중복 email → 409 Conflict")
9397
void register_duplicateEmail() throws Exception {
98+
// given: 이미 존재하는 email을 가진 User 저장
9499
User existing = User.createUser("user1", "[email protected]", "password123!");
95100
existing.setUserProfile(new UserProfile(existing, "nick1", null, null, null, 0));
96101
userRepository.save(existing);
97102

103+
// 동일 email로 회원가입 요청
98104
String body = """
99105
{
100106
"username": "otheruser",
@@ -104,6 +110,7 @@ void register_duplicateEmail() throws Exception {
104110
}
105111
""";
106112

113+
// when & then: 409 Conflict 응답 및 에러 코드 확인
107114
mvc.perform(post("/api/auth/register")
108115
.contentType(MediaType.APPLICATION_JSON)
109116
.content(body))
@@ -115,10 +122,12 @@ void register_duplicateEmail() throws Exception {
115122
@Test
116123
@DisplayName("중복 nickname → 409 Conflict")
117124
void register_duplicateNickname() throws Exception {
125+
// given: 이미 존재하는 nickname을 가진 User 저장
118126
User existing = User.createUser("user2", "[email protected]", "password123!");
119127
existing.setUserProfile(new UserProfile(existing, "dupnick", null, null, null, 0));
120128
userRepository.save(existing);
121129

130+
// 동일 nickname으로 회원가입 요청
122131
String body = """
123132
{
124133
"username": "newuser",
@@ -128,6 +137,7 @@ void register_duplicateNickname() throws Exception {
128137
}
129138
""";
130139

140+
// when & then: 409 Conflict 응답 및 에러 코드 확인
131141
mvc.perform(post("/api/auth/register")
132142
.contentType(MediaType.APPLICATION_JSON)
133143
.content(body))
@@ -139,6 +149,7 @@ void register_duplicateNickname() throws Exception {
139149
@Test
140150
@DisplayName("비밀번호 정책 위반 → 400 Bad Request")
141151
void register_invalidPassword() throws Exception {
152+
// given: 숫자/특수문자 포함 안 된 약한 비밀번호
142153
String body = """
143154
{
144155
"username": "weakpw",
@@ -148,6 +159,7 @@ void register_invalidPassword() throws Exception {
148159
}
149160
""";
150161

162+
// when & then: 400 Bad Request 응답 및 에러 코드 확인
151163
mvc.perform(post("/api/auth/register")
152164
.contentType(MediaType.APPLICATION_JSON)
153165
.content(body))
@@ -159,6 +171,7 @@ void register_invalidPassword() throws Exception {
159171
@Test
160172
@DisplayName("잘못된 요청값 (필수 필드 누락) → 400 Bad Request")
161173
void register_invalidRequest_missingField() throws Exception {
174+
// given: 필수 값 누락 (username, password, nickname 비어있음 / email 형식 잘못됨)
162175
String body = """
163176
{
164177
"username": "",
@@ -168,6 +181,7 @@ void register_invalidRequest_missingField() throws Exception {
168181
}
169182
""";
170183

184+
// when & then: 400 Bad Request 응답 및 공통 에러 코드 확인
171185
mvc.perform(post("/api/auth/register")
172186
.contentType(MediaType.APPLICATION_JSON)
173187
.content(body))

src/test/java/com/back/domain/user/service/UserServiceTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class UserServiceTest {
3838
@Test
3939
@DisplayName("정상 회원가입 성공")
4040
void register_success() {
41-
// given
41+
// given: 정상적인 회원가입 요청 생성
4242
UserRegisterRequest request = new UserRegisterRequest(
4343
"testuser", "[email protected]", "P@ssw0rd!", "홍길동"
4444
);
4545

46-
// when
46+
// when: 회원가입 실행
4747
UserResponse response = userService.register(request);
4848

49-
// then
49+
// then: 반환된 값 검증
5050
assertThat(response.username()).isEqualTo("testuser");
5151
assertThat(response.email()).isEqualTo("[email protected]");
5252
assertThat(response.nickname()).isEqualTo("홍길동");
@@ -56,17 +56,19 @@ void register_success() {
5656
String encoded = userRepository.findById(response.userId()).get().getPassword();
5757
assertThat(passwordEncoder.matches("P@ssw0rd!", encoded)).isTrue();
5858

59-
// UserProfile도 함께 저장됐는지 검증
59+
// UserProfile도 함께 생성되었는지 확인
6060
assertThat(userProfileRepository.existsByNickname("홍길동")).isTrue();
6161
}
6262

6363
@Test
6464
@DisplayName("중복된 username이면 예외 발생")
6565
void register_duplicateUsername() {
66+
// given: 동일 username으로 첫 번째 가입
6667
userService.register(new UserRegisterRequest(
6768
"dupuser", "[email protected]", "P@ssw0rd!", "닉네임"
6869
));
6970

71+
// when & then: 같은 username으로 가입 시 예외 발생
7072
assertThatThrownBy(() ->
7173
userService.register(new UserRegisterRequest(
7274
"dupuser", "[email protected]", "P@ssw0rd!", "다른닉네임"
@@ -78,10 +80,12 @@ void register_duplicateUsername() {
7880
@Test
7981
@DisplayName("중복된 email이면 예외 발생")
8082
void register_duplicateEmail() {
83+
// given: 동일 email로 첫 번째 가입
8184
userService.register(new UserRegisterRequest(
8285
"user1", "[email protected]", "P@ssw0rd!", "닉네임"
8386
));
8487

88+
// when & then: 같은 email로 가입 시 예외 발생
8589
assertThatThrownBy(() ->
8690
userService.register(new UserRegisterRequest(
8791
"user2", "[email protected]", "P@ssw0rd!", "다른닉네임"
@@ -93,10 +97,12 @@ void register_duplicateEmail() {
9397
@Test
9498
@DisplayName("중복된 nickname이면 예외 발생")
9599
void register_duplicateNickname() {
100+
// given: 동일 nickname으로 첫 번째 가입
96101
userService.register(new UserRegisterRequest(
97102
"user1", "[email protected]", "P@ssw0rd!", "dupnick"
98103
));
99104

105+
// when & then: 같은 nickname으로 가입 시 예외 발생
100106
assertThatThrownBy(() ->
101107
userService.register(new UserRegisterRequest(
102108
"user2", "[email protected]", "P@ssw0rd!", "dupnick"
@@ -108,10 +114,12 @@ void register_duplicateNickname() {
108114
@Test
109115
@DisplayName("비밀번호 정책 위반(숫자/특수문자 없음) → 예외 발생")
110116
void register_invalidPassword_noNumberOrSpecial() {
117+
// given: 숫자, 특수문자 없는 비밀번호
111118
UserRegisterRequest request = new UserRegisterRequest(
112119
"user1", "[email protected]", "abcdefgh", "닉네임"
113120
);
114121

122+
// when & then: 정책 위반으로 예외 발생
115123
assertThatThrownBy(() -> userService.register(request))
116124
.isInstanceOf(CustomException.class)
117125
.hasMessage(ErrorCode.INVALID_PASSWORD.getMessage());
@@ -120,10 +128,12 @@ void register_invalidPassword_noNumberOrSpecial() {
120128
@Test
121129
@DisplayName("비밀번호 정책 위반(길이 7자) → 예외 발생")
122130
void register_invalidPassword_tooShort() {
131+
// given: 7자리 비밀번호 (정책상 8자 이상 필요)
123132
UserRegisterRequest request = new UserRegisterRequest(
124133
"user2", "[email protected]", "Abc12!", "닉네임"
125134
);
126135

136+
// when & then: 정책 위반으로 예외 발생
127137
assertThatThrownBy(() -> userService.register(request))
128138
.isInstanceOf(CustomException.class)
129139
.hasMessage(ErrorCode.INVALID_PASSWORD.getMessage());
@@ -132,12 +142,15 @@ void register_invalidPassword_tooShort() {
132142
@Test
133143
@DisplayName("비밀번호 정책 통과(정상 8자 이상, 숫자/특수문자 포함) → 성공")
134144
void register_validPassword() {
145+
// given: 정책을 만족하는 정상 비밀번호
135146
UserRegisterRequest request = new UserRegisterRequest(
136147
"user3", "[email protected]", "Abcd123!", "닉네임"
137148
);
138149

150+
// when: 회원가입 실행
139151
UserResponse response = userService.register(request);
140152

153+
// then: username과 비밀번호 인코딩 검증
141154
assertThat(response.username()).isEqualTo("user3");
142155
assertThat(passwordEncoder.matches("Abcd123!",
143156
userRepository.findById(response.userId()).get().getPassword())).isTrue();

0 commit comments

Comments
 (0)