Skip to content

Commit e5b3787

Browse files
committed
test: 특정 다이어리 댓글 전체 조회(커서 기반) 단위 테스트 작성
1 parent adcd57c commit e5b3787

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/test/java/com/example/log4u/domain/comment/service/CommonServiceTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.*;
55
import static org.mockito.BDDMockito.*;
66

7+
import java.util.List;
78
import java.util.Optional;
89

910
import org.junit.jupiter.api.DisplayName;
@@ -12,8 +13,15 @@
1213
import org.mockito.InjectMocks;
1314
import org.mockito.Mock;
1415
import org.mockito.junit.jupiter.MockitoExtension;
16+
import org.springframework.data.domain.PageRequest;
17+
import org.springframework.data.domain.Pageable;
18+
import org.springframework.data.domain.Slice;
19+
import org.springframework.data.domain.SliceImpl;
1520

21+
import com.example.log4u.common.dto.PageResponse;
1622
import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto;
23+
import com.example.log4u.domain.comment.dto.response.CommentListResponseDto;
24+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
1725
import com.example.log4u.domain.comment.entity.Comment;
1826
import com.example.log4u.domain.comment.exception.NotFoundCommentException;
1927
import com.example.log4u.domain.comment.exception.UnauthorizedAccessException;
@@ -134,6 +142,34 @@ void commentDelete_Fail_Unauthorized() {
134142
verify(commentRepository, never()).delete(any());
135143
}
136144

145+
@DisplayName("성공 테스트: 특정 다이어리 댓글 전체 조회 (커서 기반)")
146+
@Test
147+
void getCommentsList_In_DiaryDetail_Success() {
148+
// given
149+
Long diaryId = 1L;
150+
int size = 5;
151+
Long cursorCommentId = null;
152+
153+
List<Comment> commentList = CommentFixture.createCommentsListFixture(size + 1); // hasNext 판별 위해 +1
154+
Pageable pageable = PageRequest.of(0, size);
155+
boolean hasNext = commentList.size() > size;
156+
157+
List<Comment> sliced = hasNext ? commentList.subList(0, size) : commentList;
158+
159+
Slice<Comment> slice = new SliceImpl<>(sliced, pageable, hasNext);
160+
given(commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable))
161+
.willReturn(slice);
162+
163+
// when
164+
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId, size);
165+
166+
// then
167+
assertThat(response.content()).hasSize(sliced.size());
168+
assertThat(response.pageInfo().hasNext()).isEqualTo(hasNext);
169+
assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().getCommentId() : null);
170+
171+
verify(commentRepository).findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable);
172+
}
137173

138174

139175
}

src/test/java/com/example/log4u/fixture/CommentFixture.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example.log4u.fixture;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.example.log4u.domain.comment.entity.Comment;
47

58
public class CommentFixture {
@@ -16,4 +19,19 @@ public static Comment createCommentFixture(Long commentId, Long userId, Long dia
1619
public static Comment createDefaultComment() {
1720
return createCommentFixture(1L, 1L, 1L);
1821
}
22+
23+
public static List<Comment> createCommentsListFixture(int count) {
24+
List<Comment> comments = new ArrayList<>();
25+
Long diaryId = 1L;
26+
27+
for (int i = 1; i <= count; i++) {
28+
comments.add(Comment.builder()
29+
.commentId((long) i)
30+
.userId((long) i)
31+
.diaryId(diaryId)
32+
.content("댓글" + i)
33+
.build());
34+
}
35+
return comments;
36+
}
1937
}

0 commit comments

Comments
 (0)