Skip to content

Commit e9f038a

Browse files
committed
Test: 테스트 작성
1 parent c873a42 commit e9f038a

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

src/main/java/com/back/domain/board/entity/Comment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public Comment(Post post, User user, String content) {
4141
this.user = user;
4242
this.content = content;
4343
}
44+
45+
public Comment(Post post, User user, String content, Comment parent) {
46+
this.post = post;
47+
this.user = user;
48+
this.content = content;
49+
this.parent = parent;
50+
}
4451

4552
// -------------------- 비즈니스 메서드 --------------------
4653
// 댓글 업데이트

src/test/java/com/back/domain/board/controller/CommentControllerTest.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,73 @@ void createComment_noToken() throws Exception {
207207
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
208208
}
209209

210+
// ====================== 댓글 조회 테스트 ======================
211+
212+
@Test
213+
@DisplayName("댓글 목록 조회 성공 → 200 OK")
214+
void getComments_success() throws Exception {
215+
// given
216+
User user = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
217+
user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0));
218+
user.setUserStatus(UserStatus.ACTIVE);
219+
userRepository.save(user);
220+
221+
Post post = new Post(user, "제목", "내용");
222+
postRepository.save(post);
223+
224+
// 부모 댓글
225+
Comment parent = new Comment(post, user, "부모 댓글", null);
226+
commentRepository.save(parent);
227+
228+
// 자식 댓글
229+
Comment child = new Comment(post, user, "자식 댓글", parent);
230+
commentRepository.save(child);
231+
232+
String accessToken = generateAccessToken(user);
233+
234+
// when & then
235+
mvc.perform(get("/api/posts/{postId}/comments", post.getId())
236+
.header("Authorization", "Bearer " + accessToken)
237+
.param("page", "0")
238+
.param("size", "10"))
239+
.andDo(print())
240+
.andExpect(status().isOk())
241+
.andExpect(jsonPath("$.success").value(true))
242+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
243+
.andExpect(jsonPath("$.data.items[0].content").value("부모 댓글"))
244+
.andExpect(jsonPath("$.data.items[0].children[0].content").value("자식 댓글"));
245+
}
246+
247+
@Test
248+
@DisplayName("댓글 목록 조회 실패 - 존재하지 않는 게시글 → 404 Not Found")
249+
void getComments_postNotFound() throws Exception {
250+
// given
251+
User user = User.createUser("ghost", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
252+
user.setUserProfile(new UserProfile(user, "유저", null, null, null, 0));
253+
user.setUserStatus(UserStatus.ACTIVE);
254+
userRepository.save(user);
255+
256+
String accessToken = generateAccessToken(user);
257+
258+
// when & then
259+
mvc.perform(get("/api/posts/{postId}/comments", 999L)
260+
.header("Authorization", "Bearer " + accessToken)
261+
.param("page", "0")
262+
.param("size", "10"))
263+
.andDo(print())
264+
.andExpect(status().isNotFound())
265+
.andExpect(jsonPath("$.code").value("POST_001"))
266+
.andExpect(jsonPath("$.message").value("존재하지 않는 게시글입니다."));
267+
}
268+
210269
// ====================== 댓글 수정 테스트 ======================
211270

212271
@Test
213272
@DisplayName("댓글 수정 성공 → 200 OK")
214273
void updateComment_success() throws Exception {
215274
// given: 유저 + 게시글 + 댓글
216275
User user = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
217-
user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000,1,1), 1000));
276+
user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000, 1, 1), 1000));
218277
user.setUserStatus(UserStatus.ACTIVE);
219278
userRepository.save(user);
220279

@@ -390,6 +449,7 @@ void updateComment_noToken() throws Exception {
390449
.andExpect(jsonPath("$.code").value("AUTH_001"))
391450
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
392451
}
452+
393453
// ====================== 댓글 삭제 테스트 ======================
394454

395455
@Test

src/test/java/com/back/domain/board/service/CommentServiceTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.back.domain.board.service;
22

3+
import com.back.domain.board.dto.CommentListResponse;
34
import com.back.domain.board.dto.CommentRequest;
45
import com.back.domain.board.dto.CommentResponse;
6+
import com.back.domain.board.dto.PageResponse;
57
import com.back.domain.board.entity.Comment;
68
import com.back.domain.board.entity.Post;
79
import com.back.domain.board.repository.CommentRepository;
@@ -16,6 +18,9 @@
1618
import org.junit.jupiter.api.Test;
1719
import org.springframework.beans.factory.annotation.Autowired;
1820
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.data.domain.PageRequest;
22+
import org.springframework.data.domain.Pageable;
23+
import org.springframework.data.domain.Sort;
1924
import org.springframework.test.context.ActiveProfiles;
2025
import org.springframework.transaction.annotation.Transactional;
2126

@@ -100,6 +105,52 @@ void createComment_fail_postNotFound() {
100105
.hasMessage(ErrorCode.POST_NOT_FOUND.getMessage());
101106
}
102107

108+
// ====================== 댓글 조회 테스트 ======================
109+
110+
@Test
111+
@DisplayName("댓글 목록 조회 성공 - 부모 + 자식 포함")
112+
void getComments_success() {
113+
// given: 유저 + 게시글
114+
User user = User.createUser("writer", "[email protected]", "pwd");
115+
user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0));
116+
user.setUserStatus(UserStatus.ACTIVE);
117+
userRepository.save(user);
118+
119+
Post post = new Post(user, "제목", "내용");
120+
postRepository.save(post);
121+
122+
// 부모 댓글
123+
Comment parent = new Comment(post, user, "부모 댓글", null);
124+
commentRepository.save(parent);
125+
126+
// 자식 댓글
127+
Comment child = new Comment(post, user, "자식 댓글", parent);
128+
commentRepository.save(child);
129+
130+
Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.ASC, "createdAt"));
131+
132+
// when
133+
PageResponse<CommentListResponse> response = commentService.getComments(post.getId(), pageable);
134+
135+
// then
136+
assertThat(response.items()).hasSize(1); // 부모만 페이징 결과
137+
CommentListResponse parentRes = response.items().getFirst();
138+
assertThat(parentRes.getContent()).isEqualTo("부모 댓글");
139+
assertThat(parentRes.getChildren()).hasSize(1);
140+
assertThat(parentRes.getChildren().getFirst().getContent()).isEqualTo("자식 댓글");
141+
}
142+
143+
@Test
144+
@DisplayName("댓글 목록 조회 실패 - 게시글 없음")
145+
void getComments_fail_postNotFound() {
146+
Pageable pageable = PageRequest.of(0, 10);
147+
148+
assertThatThrownBy(() ->
149+
commentService.getComments(999L, pageable)
150+
).isInstanceOf(CustomException.class)
151+
.hasMessage(ErrorCode.POST_NOT_FOUND.getMessage());
152+
}
153+
103154
// ====================== 댓글 수정 테스트 ======================
104155

105156
@Test
@@ -200,6 +251,7 @@ void updateComment_fail_noPermission() {
200251
).isInstanceOf(CustomException.class)
201252
.hasMessage(ErrorCode.COMMENT_NO_PERMISSION.getMessage());
202253
}
254+
203255
// ====================== 댓글 삭제 테스트 ======================
204256

205257
@Test

0 commit comments

Comments
 (0)