Skip to content

Commit fe0f0f8

Browse files
committed
test: 좋아요 추가 API 테스트 코드 설명, 예외 처리 알맞게 수정
1 parent 6163d80 commit fe0f0f8

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

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

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

7+
import java.util.Optional;
8+
79
import org.junit.jupiter.api.DisplayName;
810
import org.junit.jupiter.api.Test;
911
import org.junit.jupiter.api.extension.ExtendWith;
@@ -12,10 +14,13 @@
1214
import org.mockito.junit.jupiter.MockitoExtension;
1315

1416
import com.example.log4u.domain.diary.entity.Diary;
17+
import com.example.log4u.domain.diary.exception.NotFoundDiaryException;
1518
import com.example.log4u.domain.diary.service.DiaryService;
1619
import com.example.log4u.domain.like.dto.request.LikeAddRequestDto;
1720
import com.example.log4u.domain.like.dto.response.LikeAddResponseDto;
21+
import com.example.log4u.domain.like.dto.response.LikeCancelResponseDto;
1822
import com.example.log4u.domain.like.entity.Like;
23+
import com.example.log4u.domain.like.exception.DuplicateLikeException;
1924
import com.example.log4u.domain.like.repository.LikeRepository;
2025
import com.example.log4u.domain.user.entity.User;
2126
import com.example.log4u.fixture.DiaryFixture;
@@ -36,22 +41,22 @@ public class LikeServiceTest {
3641
private DiaryService diaryService;
3742

3843
@Test
39-
@DisplayName("성공 테스트: 사용자가 게시물에 좋아요를 누르면 좋아요가 저장된다")
44+
@DisplayName("성공 테스트: 좋아요 추가 ")
4045
void likeSuccess() {
4146
// given
42-
User user = UserFixture.createUserFixture();
43-
Diary diary = DiaryFixture.createDiaryFixture();
44-
LikeAddRequestDto requestDto = new LikeAddRequestDto(diary.getDiaryId());
47+
Long userId = 1L;
48+
Long diaryId = 123L;
49+
LikeAddRequestDto requestDto = new LikeAddRequestDto(diaryId);
4550

46-
Like like = LikeFixture.createLikeFixture(123243L, user.getUserId(), diary.getDiaryId());
51+
Like like = LikeFixture.createLikeFixture(123243L, userId, diaryId);
4752
Long updatedLikeCount = 11L;
4853

49-
given(likeRepository.existsByUserIdAndDiaryId(user.getUserId(), diary.getDiaryId())).willReturn(false);
54+
given(likeRepository.existsByUserIdAndDiaryId(userId, diaryId)).willReturn(false);
5055
given(likeRepository.save(any(Like.class))).willReturn(like);
51-
given(diaryService.incrementLikeCount(diary.getDiaryId())).willReturn(updatedLikeCount);
56+
given(diaryService.incrementLikeCount(diaryId)).willReturn(updatedLikeCount);
5257

5358
// when
54-
LikeAddResponseDto response = likeService.addLike(user.getUserId(), requestDto);
59+
LikeAddResponseDto response = likeService.addLike(userId, requestDto);
5560

5661
// then
5762
verify(likeRepository).save(any(Like.class));
@@ -60,26 +65,26 @@ void likeSuccess() {
6065
}
6166

6267
@Test
63-
@DisplayName("예외 테스트: 존재하지 않는 다이어리에 좋아요 요청 시 예외가 발생한다")
68+
@DisplayName("예외 테스트: 좋아요 추가 - 존재하지 않는 다이어리에 좋아요 요청")
6469
void likeFail_whenDiaryNotFound() {
6570
// given
6671
Long userId = 1L;
6772
Long diaryId = 100L;
6873
LikeAddRequestDto requestDto = new LikeAddRequestDto(diaryId);
6974

7075
given(likeRepository.existsByUserIdAndDiaryId(userId, diaryId)).willReturn(false);
71-
given(diaryService.incrementLikeCount(diaryId)).willThrow(new IllegalArgumentException());
76+
given(diaryService.incrementLikeCount(diaryId)).willThrow(new NotFoundDiaryException());
7277

7378
// when & then
74-
assertThrows(IllegalArgumentException.class, () -> {
79+
assertThrows(NotFoundDiaryException.class, () -> {
7580
likeService.addLike(userId, requestDto);
7681
});
7782

7883
verify(likeRepository).save(any(Like.class));
7984
}
8085

8186
@Test
82-
@DisplayName("예외 테스트: 사용자가 이미 좋아요를 누른 다이어리에요청하면 예외가 발생한다")
87+
@DisplayName("예외 테스트: 좋아요 추가 - 이미 누른 좋아요요청")
8388
void likeFail_whenAlreadyLiked() {
8489
// given
8590
Long userId = 1L;
@@ -89,7 +94,7 @@ void likeFail_whenAlreadyLiked() {
8994
given(likeRepository.existsByUserIdAndDiaryId(userId, diaryId)).willReturn(true);
9095

9196
// when & then
92-
assertThrows(IllegalArgumentException.class, () -> {
97+
assertThrows(DuplicateLikeException.class, () -> {
9398
likeService.addLike(userId, requestDto);
9499
});
95100

0 commit comments

Comments
 (0)