blob: f76880a6dedd0303df8ffcf9a575af378d91da8e [file] [log] [blame]
夜雨声烦5c9d1312025-04-23 17:46:19 +08001package com.example.g8backend.service;
2
3import com.example.g8backend.entity.Comment;
4import com.example.g8backend.mapper.CommentMapper;
5import com.example.g8backend.service.impl.CommentServiceImpl;
6import org.junit.jupiter.api.BeforeEach;
7import org.junit.jupiter.api.DisplayName;
8import org.junit.jupiter.api.Test;
9import org.junit.jupiter.api.extension.ExtendWith;
10import org.mockito.InjectMocks;
11import org.mockito.Mock;
12import org.mockito.MockitoAnnotations;
13import org.springframework.test.context.junit.jupiter.SpringExtension;
14
15import java.util.Arrays;
16import java.util.List;
17
18import static org.junit.jupiter.api.Assertions.*;
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.Mockito.*;
21
22@ExtendWith(SpringExtension.class)
23@DisplayName("评论服务测试")
24class CommentServiceTest {
25
26 @Mock
27 private CommentMapper commentMapper;
28
29 @InjectMocks
30 private CommentServiceImpl commentService;
31
32 private Comment testComment;
33
34 @BeforeEach
35 void setUp() {
36 MockitoAnnotations.openMocks(this);
37 testComment = createTestComment();
38 }
39
40 private Comment createTestComment() {
41 Comment comment = new Comment();
42 comment.setCommentId(1L);
43 comment.setPostId(1L);
44 comment.setUserId(1L);
45 comment.setContent("测试评论内容");
46 return comment;
47 }
48
49 @Test
50 @DisplayName("创建评论-成功")
51 void createComment_ShouldSucceed() {
52 // Arrange
53 when(commentMapper.insert(any(Comment.class))).thenReturn(1);
54
55 // Act
56 commentService.createComment(1L, 1L, "测试评论内容", null);
57
58 // Assert
59 verify(commentMapper).insert(any(Comment.class));
60 }
61
62 @Test
63 @DisplayName("获取评论-根据帖子ID返回评论列表")
64 void getCommentsByPostId_ShouldReturnComments() {
65 // Arrange
66 List<Comment> expectedComments = Arrays.asList(testComment);
67 when(commentMapper.findTopLevelCommentsByPostId(1L)).thenReturn(expectedComments);
68
69 // Act
70 List<Comment> result = commentService.getCommentsByPostId(1L);
71
72 // Assert
73 assertNotNull(result);
74 assertFalse(result.isEmpty());
75 assertEquals(testComment.getCommentId(), result.get(0).getCommentId());
76 verify(commentMapper).findTopLevelCommentsByPostId(1L);
77 }
78
79 @Test
80 @DisplayName("删除评论-成功")
81 void deleteComment_ShouldSucceed() {
82 // Arrange
83 when(commentMapper.deleteById(1L)).thenReturn(1);
84
85 // Act
86 boolean result = commentService.deleteComment(1L);
87
88 // Assert
89 assertTrue(result);
90 verify(commentMapper).deleteById(1L);
91 }
92
93 @Test
94 @DisplayName("获取评论-没有评论")
95 void getCommentsByPostId_WhenNoComments_ShouldReturnEmptyList() {
96 // Arrange
97 when(commentMapper.findTopLevelCommentsByPostId(999L)).thenReturn(Arrays.asList());
98
99 // Act
100 List<Comment> result = commentService.getCommentsByPostId(999L);
101
102 // Assert
103 assertNotNull(result);
104 assertTrue(result.isEmpty());
105 verify(commentMapper).findTopLevelCommentsByPostId(999L);
106 }
107
108 @Test
109 @DisplayName("获取评论-根据父评论ID返回子评论")
110 void getReplies_ShouldReturnReplies() {
111 // Arrange
112 List<Comment> expectedReplies = Arrays.asList(testComment);
113 when(commentMapper.findRepliesByParentCommentId(1L)).thenReturn(expectedReplies);
114
115 // Act
116 List<Comment> result = commentService.getReplies(1L, 5);
117
118 // Assert
119 assertNotNull(result);
120 assertFalse(result.isEmpty());
121 assertEquals(testComment.getCommentId(), result.get(0).getCommentId());
122 verify(commentMapper).findRepliesByParentCommentId(1L);
123 }
124
125}