Skip to content

Commit 8dc3335

Browse files
committed
test: 댓글 생성 API 단위 테스트 작성
1 parent 7ff94e6 commit 8dc3335

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.log4u.fixture;
2+
3+
import com.example.log4u.domain.comment.entity.Comment;
4+
5+
public class CommentFixture {
6+
7+
public static Comment createCommentFixture(Long commentId, Long userId, Long diaryId) {
8+
return Comment.builder()
9+
.commentId(commentId)
10+
.userId(userId)
11+
.diaryId(diaryId)
12+
.content("댓글 내용")
13+
.build();
14+
}
15+
16+
public static Comment createDefaultComment() {
17+
return createCommentFixture(1L, 1L, 1L);
18+
}
19+
}

0 commit comments

Comments
 (0)