|
| 1 | +package com.example.log4u.domain.like.service; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | +import static org.mockito.BDDMockito.*; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.mockito.InjectMocks; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | + |
| 14 | +import com.example.log4u.domain.diary.entity.Diary; |
| 15 | +import com.example.log4u.domain.diary.service.DiaryService; |
| 16 | +import com.example.log4u.domain.like.dto.request.LikeAddRequestDto; |
| 17 | +import com.example.log4u.domain.like.dto.response.LikeAddResponseDto; |
| 18 | +import com.example.log4u.domain.like.entity.Like; |
| 19 | +import com.example.log4u.domain.like.repository.LikeRepository; |
| 20 | +import com.example.log4u.domain.user.entity.User; |
| 21 | +import com.example.log4u.fixture.DiaryFixture; |
| 22 | +import com.example.log4u.fixture.LikeFixture; |
| 23 | +import com.example.log4u.fixture.UserFixture; |
| 24 | + |
| 25 | +@DisplayName("좋아요 API 단위 테스트") |
| 26 | +@ExtendWith(MockitoExtension.class) |
| 27 | +public class LikeServiceTest { |
| 28 | + |
| 29 | + @InjectMocks |
| 30 | + private LikeService likeService; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private LikeRepository likeRepository; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private DiaryService diaryService; |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("성공 테스트: 사용자가 게시물에 좋아요를 누르면 좋아요가 저장된다") |
| 40 | + void likeSuccess() { |
| 41 | + // given |
| 42 | + User user = UserFixture.createUserFixture(); |
| 43 | + Diary diary = DiaryFixture.createDiaryFixture(); |
| 44 | + LikeAddRequestDto requestDto = new LikeAddRequestDto(diary.getDiaryId()); |
| 45 | + |
| 46 | + Like like = LikeFixture.createLikeFixture(123243L, user.getUserId(), diary.getDiaryId()); |
| 47 | + Long updatedLikeCount = 11L; |
| 48 | + |
| 49 | + given(likeRepository.existsByUserIdAndDiaryId(user.getUserId(), diary.getDiaryId())).willReturn(false); |
| 50 | + given(likeRepository.save(any(Like.class))).willReturn(like); |
| 51 | + given(diaryService.incrementLikeCount(diary.getDiaryId())).willReturn(updatedLikeCount); |
| 52 | + |
| 53 | + // when |
| 54 | + LikeAddResponseDto response = likeService.addLike(user.getUserId(), requestDto); |
| 55 | + |
| 56 | + // then |
| 57 | + verify(likeRepository).save(any(Like.class)); |
| 58 | + assertThat(response.liked()).isTrue(); |
| 59 | + assertThat(response.likeCount()).isEqualTo(updatedLikeCount); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @DisplayName("예외 테스트: 존재하지 않는 다이어리에 좋아요 요청 시 예외가 발생한다") |
| 64 | + void likeFail_whenDiaryNotFound() { |
| 65 | + // given |
| 66 | + Long userId = 1L; |
| 67 | + Long diaryId = 100L; |
| 68 | + LikeAddRequestDto requestDto = new LikeAddRequestDto(diaryId); |
| 69 | + |
| 70 | + given(likeRepository.existsByUserIdAndDiaryId(userId, diaryId)).willReturn(false); |
| 71 | + given(diaryService.incrementLikeCount(diaryId)).willThrow(new IllegalArgumentException()); |
| 72 | + |
| 73 | + // when & then |
| 74 | + assertThrows(IllegalArgumentException.class, () -> { |
| 75 | + likeService.addLike(userId, requestDto); |
| 76 | + }); |
| 77 | + |
| 78 | + verify(likeRepository).save(any(Like.class)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("예외 테스트: 사용자가 이미 좋아요를 누른 다이어리에 또 요청하면 예외가 발생한다") |
| 83 | + void likeFail_whenAlreadyLiked() { |
| 84 | + // given |
| 85 | + Long userId = 1L; |
| 86 | + Long diaryId = 100L; |
| 87 | + LikeAddRequestDto requestDto = new LikeAddRequestDto(diaryId); |
| 88 | + |
| 89 | + given(likeRepository.existsByUserIdAndDiaryId(userId, diaryId)).willReturn(true); |
| 90 | + |
| 91 | + // when & then |
| 92 | + assertThrows(IllegalArgumentException.class, () -> { |
| 93 | + likeService.addLike(userId, requestDto); |
| 94 | + }); |
| 95 | + |
| 96 | + verify(likeRepository, never()).save(any(Like.class)); |
| 97 | + } |
| 98 | +} |
0 commit comments