Skip to content

Commit e237e94

Browse files
committed
test: 좋아요 취소 API 단위 테스트 작성
1 parent a97d94f commit e237e94

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/test/java/com/example/log4u/domain/like/service/LikeServiceTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,50 @@ void likeFail_whenAlreadyLiked() {
100100

101101
verify(likeRepository, never()).save(any(Like.class));
102102
}
103+
104+
@DisplayName("성공 테스트: 좋아요 취소")
105+
@Test
106+
void cancelLike_Success() {
107+
Long userId = 1L;
108+
Long diaryId = 123L;
109+
110+
Like like = LikeFixture.createLikeFixture(123243L, userId, diaryId);
111+
Long updatedLikeCount = 11L;
112+
113+
given(likeRepository.findByUserIdAndDiaryId(userId, diaryId)).willReturn(Optional.of(like));
114+
doNothing().when(likeRepository).delete(like);
115+
given(diaryService.decreaseLikeCount(diaryId)).willReturn(updatedLikeCount);
116+
117+
// when
118+
LikeCancelResponseDto response = likeService.cancelLike(userId, diaryId);
119+
120+
// then
121+
verify(likeRepository).delete(like);
122+
verify(diaryService).decreaseLikeCount(diaryId);
123+
124+
assertThat(response.liked()).isFalse();
125+
assertThat(response.likeCount()).isEqualTo(updatedLikeCount);
126+
}
127+
128+
@DisplayName("성공 테스트: 좋아요 취소 - 존재하지 않는 좋아요에 대해 동일한 결과 반환)")
129+
@Test
130+
void cancelLike_NoLikeExists() {
131+
// given
132+
Long userId = 1L;
133+
Long diaryId = 100L;
134+
Long currentCount = 5L;
135+
136+
given(likeRepository.findByUserIdAndDiaryId(userId, diaryId)).willReturn(Optional.empty());
137+
given(diaryService.getLikeCount(diaryId)).willReturn(currentCount);
138+
139+
// when
140+
LikeCancelResponseDto response = likeService.cancelLike(userId, diaryId);
141+
142+
// then
143+
verify(likeRepository, never()).delete(any());
144+
verify(diaryService).getLikeCount(diaryId);
145+
146+
assertThat(response.liked()).isFalse();
147+
assertThat(response.likeCount()).isEqualTo(currentCount);
148+
}
103149
}

0 commit comments

Comments
 (0)