|
| 1 | +package com.example.log4u.domain.comment.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 java.util.Optional; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto; |
| 17 | +import com.example.log4u.domain.comment.entity.Comment; |
| 18 | +import com.example.log4u.domain.comment.exception.NotFoundCommentException; |
| 19 | +import com.example.log4u.domain.comment.exception.UnauthorizedAccessException; |
| 20 | +import com.example.log4u.domain.comment.repository.CommentRepository; |
| 21 | +import com.example.log4u.domain.diary.exception.NotFoundDiaryException; |
| 22 | +import com.example.log4u.domain.diary.service.DiaryService; |
| 23 | +import com.example.log4u.domain.comment.dto.response.CommentCreateResponseDto; |
| 24 | +import com.example.log4u.fixture.CommentFixture; |
| 25 | + |
| 26 | +@DisplayName("댓글 API 단위 테스트") |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +public class CommonServiceTest { |
| 29 | + |
| 30 | + @InjectMocks |
| 31 | + private CommentService commentService; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private CommentRepository commentRepository; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private DiaryService diaryService; |
| 38 | + |
| 39 | + @DisplayName("성공 테스트: 댓글 생성") |
| 40 | + @Test |
| 41 | + void commentCreate_Success() { |
| 42 | + // given |
| 43 | + Long userId = 1L; |
| 44 | + Long diaryId = 12L; |
| 45 | + |
| 46 | + CommentCreateRequestDto requestDto = new CommentCreateRequestDto(diaryId, "댓글 내용"); |
| 47 | + Comment expectedComment = CommentFixture.createCommentFixture(100L, userId, diaryId); |
| 48 | + |
| 49 | + doNothing().when(diaryService).checkDiaryExists(diaryId); |
| 50 | + given(commentRepository.save(any(Comment.class))).willReturn(expectedComment); |
| 51 | + |
| 52 | + // when |
| 53 | + CommentCreateResponseDto response = commentService.addComment(userId, requestDto); |
| 54 | + |
| 55 | + // then |
| 56 | + assertThat(response.content()).isEqualTo(expectedComment.getContent()); |
| 57 | + |
| 58 | + verify(commentRepository).save(any(Comment.class)); |
| 59 | + } |
| 60 | + |
| 61 | + @DisplayName("예외 테스트: 댓글 생성 - 존재하지 않는 다이어리에 댓글 생성 시 예외") |
| 62 | + @Test |
| 63 | + void commentCreate_Fail_DiaryNotFound() { |
| 64 | + // given |
| 65 | + Long userId = 1L; |
| 66 | + Long diaryId = 999L; |
| 67 | + CommentCreateRequestDto requestDto = new CommentCreateRequestDto(diaryId, "댓글 내용"); |
| 68 | + |
| 69 | + doThrow(new NotFoundDiaryException()) |
| 70 | + .when(diaryService).checkDiaryExists(diaryId); |
| 71 | + |
| 72 | + // when & then |
| 73 | + assertThrows(NotFoundDiaryException.class, () -> { |
| 74 | + commentService.addComment(userId, requestDto); |
| 75 | + }); |
| 76 | + |
| 77 | + verify(commentRepository, never()).save(any()); |
| 78 | + } |
| 79 | +} |
0 commit comments