Skip to content

Commit 2bbcd8e

Browse files
committed
test: 특정 다이어리 댓글 전체 조회 API 단위 테스트 수정
1 parent a8f4f25 commit 2bbcd8e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88
import java.util.Optional;
9+
import java.util.stream.IntStream;
910

1011
import org.junit.jupiter.api.DisplayName;
1112
import org.junit.jupiter.api.Test;
@@ -149,26 +150,26 @@ void getCommentsList_In_DiaryDetail_Success() {
149150
int size = 5;
150151
Long cursorCommentId = null;
151152

152-
List<Comment> commentList = CommentFixture.createCommentsListFixture(size + 1); // hasNext 판별 위해 +1
153+
List<CommentResponseDto> dtoList = CommentFixture.createCommentDtos(size + 1);
154+
153155
Pageable pageable = PageRequest.of(0, size);
154-
boolean hasNext = commentList.size() > size;
156+
boolean hasNext = dtoList.size() > size;
157+
List<CommentResponseDto> sliced = hasNext ? dtoList.subList(0, size) : dtoList;
155158

156-
List<Comment> sliced = hasNext ? commentList.subList(0, size) : commentList;
159+
Slice<CommentResponseDto> slice = new SliceImpl<>(sliced, pageable, hasNext);
157160

158-
Slice<Comment> slice = new SliceImpl<>(sliced, pageable, hasNext);
159-
given(commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable))
161+
given(commentRepository.findWithUserByDiaryId(diaryId, cursorCommentId, pageable))
160162
.willReturn(slice);
161163

162164
// when
163-
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId,
164-
size);
165+
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId, size);
165166

166167
// then
167168
assertThat(response.list()).hasSize(sliced.size());
168169
assertThat(response.pageInfo().hasNext()).isEqualTo(hasNext);
169-
assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().getCommentId() : null);
170+
assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().commentId() : null);
170171

171-
verify(commentRepository).findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable);
172+
verify(commentRepository).findWithUserByDiaryId(diaryId, cursorCommentId, pageable);
172173
}
173174

174175
}

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

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

3+
import java.time.LocalDateTime;
34
import java.util.ArrayList;
45
import java.util.List;
6+
import java.util.stream.IntStream;
57

8+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
69
import com.example.log4u.domain.comment.entity.Comment;
710

811
public class CommentFixture {
@@ -34,4 +37,16 @@ public static List<Comment> createCommentsListFixture(int count) {
3437
}
3538
return comments;
3639
}
40+
41+
public static List<CommentResponseDto> createCommentDtos(int size) {
42+
return IntStream.rangeClosed(1, size)
43+
.mapToObj(i -> new CommentResponseDto(
44+
(long) i,
45+
(long) i,
46+
"사용자" + i,
47+
"https://cdn.example.com/user" + i + ".png",
48+
"댓글 " + i,
49+
LocalDateTime.now().minusMinutes(i) // createdAt
50+
)).toList();
51+
}
3752
}

0 commit comments

Comments
 (0)