Skip to content

Commit 9421bb6

Browse files
committed
[Test]: 댓글 목록 조회 테스트 구현
1 parent d3357ce commit 9421bb6

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

back/src/main/java/com/back/domain/comment/service/CommentService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public CommentResponse createComment(Long userId, Long postId, CommentRequest re
4343
}
4444

4545
public Page<CommentResponse> getComments(Long userId, Long postId, Pageable pageable) {
46+
Post post = postRepository.findById(postId)
47+
.orElseThrow(() -> new ApiException(ErrorCode.POST_NOT_FOUND));
4648
Page<Comment> commentsPage = commentRepository.findCommentsByPostId(postId, pageable);
4749
return commentsPage.map(CommentMappers.COMMENT_READ::map);
4850
}

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

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.domain.comment.controller;
22

33
import com.back.domain.comment.dto.CommentRequest;
4+
import com.back.domain.comment.entity.Comment;
45
import com.back.domain.comment.repository.CommentRepository;
56
import com.back.domain.post.entity.Post;
67
import com.back.domain.post.repository.PostRepository;
@@ -18,10 +19,13 @@
1819
import org.springframework.test.context.ActiveProfiles;
1920
import org.springframework.test.web.servlet.MockMvc;
2021
import org.springframework.transaction.annotation.Transactional;
22+
import org.springframework.util.ReflectionUtils;
2123

24+
import java.lang.reflect.Field;
2225
import java.time.LocalDateTime;
2326
import java.util.UUID;
2427

28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2529
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2630
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
2731
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -95,4 +99,180 @@ void success() throws Exception {
9599
.andDo(print());
96100
}
97101
}
102+
103+
@Nested
104+
@DisplayName("댓글 목록 조회")
105+
class GetComments {
106+
107+
@Test
108+
@DisplayName("성공 - 기본 파라미터로 댓글 목록 조회")
109+
void success() throws Exception {
110+
// Given - 테스트 댓글들 생성
111+
createTestComments();
112+
113+
// When & Then
114+
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
115+
.param("userId", String.valueOf(testUser.getId()))
116+
.contentType(MediaType.APPLICATION_JSON))
117+
.andExpect(status().isOk())
118+
.andExpect(jsonPath("$.message").value("성공적으로 조회되었습니다."))
119+
.andExpect(jsonPath("$.data.items").isArray())
120+
.andExpect(jsonPath("$.data.items.length()").value(3))
121+
.andExpect(jsonPath("$.data.totalElements").value(3))
122+
.andExpect(jsonPath("$.data.totalPages").value(1))
123+
.andDo(print());
124+
}
125+
126+
@Test
127+
@DisplayName("성공 - LATEST 정렬로 댓글 목록 조회")
128+
void successWithLatestSort() throws Exception {
129+
// Given
130+
createTestCommentsWithDifferentTimes();
131+
132+
// When & Then
133+
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
134+
.param("userId", String.valueOf(testUser.getId()))
135+
.param("sortType", "LATEST")
136+
.contentType(MediaType.APPLICATION_JSON))
137+
.andExpect(status().isOk())
138+
.andExpect(jsonPath("$.data.items[0].content").value("가장 최근 댓글"))
139+
.andExpect(jsonPath("$.data.items[1].content").value("중간 댓글"))
140+
.andExpect(jsonPath("$.data.items[2].content").value("오래된 댓글"))
141+
.andDo(print());
142+
}
143+
144+
@Test
145+
@DisplayName("성공 - LIKES 정렬로 댓글 목록 조회")
146+
void successWithLikesSort() throws Exception {
147+
// Given
148+
createTestCommentsWithDifferentLikes();
149+
150+
// When & Then
151+
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
152+
.param("userId", String.valueOf(testUser.getId()))
153+
.param("sortType", "LIKES")
154+
.contentType(MediaType.APPLICATION_JSON))
155+
.andExpect(status().isOk())
156+
.andExpect(jsonPath("$.data.items[0].likeCount").value(10))
157+
.andExpect(jsonPath("$.data.items[1].likeCount").value(5))
158+
.andExpect(jsonPath("$.data.items[2].likeCount").value(2))
159+
.andDo(print());
160+
}
161+
162+
@Test
163+
@DisplayName("성공 - 빈 댓글 목록 조회")
164+
void successWithEmptyComments() throws Exception {
165+
// When & Then
166+
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
167+
.param("userId", String.valueOf(testUser.getId()))
168+
.contentType(MediaType.APPLICATION_JSON))
169+
.andExpect(status().isOk())
170+
.andExpect(jsonPath("$.data.items").isArray())
171+
.andExpect(jsonPath("$.data.items.length()").value(0))
172+
.andExpect(jsonPath("$.data.totalElements").value(0))
173+
.andExpect(jsonPath("$.data.totalPages").value(0))
174+
.andDo(print());
175+
}
176+
177+
@Test
178+
@DisplayName("실패 - 존재하지 않는 게시글 ID")
179+
void failWithNonExistentPostId() throws Exception {
180+
// When & Then
181+
mockMvc.perform(get("/api/v1/posts/{postId}/comments", 999L)
182+
.param("userId", String.valueOf(testUser.getId()))
183+
.contentType(MediaType.APPLICATION_JSON))
184+
.andExpect(status().isNotFound())
185+
.andDo(print());
186+
}
187+
188+
}
189+
190+
private void createTestComments() {
191+
for (int i = 1; i <= 3; i++) {
192+
Comment comment = Comment.builder()
193+
.content("테스트 댓글 " + i)
194+
.post(testPost)
195+
.user(testUser)
196+
.hide(false)
197+
.build();
198+
commentRepository.save(comment);
199+
}
200+
}
201+
202+
private void createTestCommentsWithDifferentTimes() {
203+
// 오래된 댓글
204+
Comment oldComment = Comment.builder()
205+
.content("오래된 댓글")
206+
.post(testPost)
207+
.user(testUser)
208+
.hide(false)
209+
.build();
210+
Field createdDateOldField = ReflectionUtils.findField(oldComment.getClass(), "createdDate");
211+
ReflectionUtils.makeAccessible(createdDateOldField);
212+
ReflectionUtils.setField(createdDateOldField, oldComment, LocalDateTime.now().minusDays(1));
213+
commentRepository.save(oldComment);
214+
215+
// 중간 댓글
216+
Comment middleComment = Comment.builder()
217+
.content("중간 댓글")
218+
.post(testPost)
219+
.user(testUser)
220+
.hide(false)
221+
.build();
222+
223+
Field createdDateField = ReflectionUtils.findField(middleComment.getClass(), "createdDate");
224+
ReflectionUtils.makeAccessible(createdDateField);
225+
ReflectionUtils.setField(createdDateField, middleComment, LocalDateTime.now().minusDays(1));
226+
commentRepository.save(middleComment);
227+
228+
// 최근 댓글
229+
Comment recentComment = Comment.builder()
230+
.content("가장 최근 댓글")
231+
.post(testPost)
232+
.user(testUser)
233+
.hide(false)
234+
.build();
235+
commentRepository.save(recentComment);
236+
}
237+
238+
private void createTestCommentsWithDifferentLikes() {
239+
Comment comment1 = Comment.builder()
240+
.content("좋아요 2개")
241+
.post(testPost)
242+
.user(testUser)
243+
.hide(false)
244+
.likeCount(2)
245+
.build();
246+
commentRepository.save(comment1);
247+
248+
Comment comment2 = Comment.builder()
249+
.content("좋아요 5개")
250+
.post(testPost)
251+
.user(testUser)
252+
.hide(false)
253+
.likeCount(5)
254+
.build();
255+
commentRepository.save(comment2);
256+
257+
Comment comment3 = Comment.builder()
258+
.content("좋아요 10개")
259+
.post(testPost)
260+
.user(testUser)
261+
.hide(false)
262+
.likeCount(10)
263+
.build();
264+
commentRepository.save(comment3);
265+
}
266+
267+
private void createManyTestComments(int count) {
268+
for (int i = 1; i <= count; i++) {
269+
Comment comment = Comment.builder()
270+
.content("댓글 " + i)
271+
.post(testPost)
272+
.user(testUser)
273+
.hide(false)
274+
.build();
275+
commentRepository.save(comment);
276+
}
277+
}
98278
}

0 commit comments

Comments
 (0)