Skip to content

Commit a9cdfa2

Browse files
committed
test: 댓글 삭제 API 단위 테스트 작성
1 parent 6e0a17a commit a9cdfa2

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,64 @@ void commentCreate_Fail_DiaryNotFound() {
7676

7777
verify(commentRepository, never()).save(any());
7878
}
79+
80+
@DisplayName("성공 테스트: 댓글 삭제")
81+
@Test
82+
void commentDelete_Success() {
83+
// given
84+
Long userId = 1L;
85+
Long commentId = 100L;
86+
Long diaryId = 12L;
87+
88+
Comment comment = CommentFixture.createCommentFixture(commentId, userId, diaryId);
89+
90+
given(commentRepository.findById(commentId)).willReturn(Optional.of(comment));
91+
92+
// when
93+
commentService.deleteComment(userId, commentId);
94+
95+
// then
96+
verify(commentRepository).delete(comment);
97+
}
98+
99+
@DisplayName("예외 테스트: 댓글 삭제 - 존재하지 않는 댓글")
100+
@Test
101+
void commentDelete_Fail_NotFound() {
102+
// given
103+
Long userId = 1L;
104+
Long commentId = 999L;
105+
106+
given(commentRepository.findById(commentId)).willReturn(Optional.empty());
107+
108+
// when & then
109+
assertThrows(NotFoundCommentException.class, () -> {
110+
commentService.deleteComment(userId, commentId);
111+
});
112+
113+
verify(commentRepository, never()).delete(any());
114+
}
115+
116+
@DisplayName("예외 테스트: 댓글 삭제 - 본인 댓글이 아닌 사용자의 삭제 요청")
117+
@Test
118+
void commentDelete_Fail_Unauthorized() {
119+
// given
120+
Long commentId = 100L;
121+
Long commentOwnerId = 1L;
122+
Long otherUserId = 2L;
123+
Long diaryId = 12L;
124+
125+
Comment comment = CommentFixture.createCommentFixture(commentId, commentOwnerId, diaryId);
126+
127+
given(commentRepository.findById(commentId)).willReturn(Optional.of(comment));
128+
129+
// when & then
130+
assertThrows(UnauthorizedAccessException.class, () -> {
131+
commentService.deleteComment(otherUserId, commentId);
132+
});
133+
134+
verify(commentRepository, never()).delete(any());
135+
}
136+
137+
138+
79139
}

0 commit comments

Comments
 (0)