blob: 96d44e9af498173ceaf8600d50647a112582556a [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
夜雨声烦5c9d1312025-04-23 17:46:19 +080049
50 @Test
51 @DisplayName("获取评论-根据帖子ID返回评论列表")
52 void getCommentsByPostId_ShouldReturnComments() {
53 // Arrange
54 List<Comment> expectedComments = Arrays.asList(testComment);
55 when(commentMapper.findTopLevelCommentsByPostId(1L)).thenReturn(expectedComments);
56
57 // Act
58 List<Comment> result = commentService.getCommentsByPostId(1L);
59
60 // Assert
61 assertNotNull(result);
62 assertFalse(result.isEmpty());
63 assertEquals(testComment.getCommentId(), result.get(0).getCommentId());
64 verify(commentMapper).findTopLevelCommentsByPostId(1L);
65 }
66
67 @Test
68 @DisplayName("删除评论-成功")
69 void deleteComment_ShouldSucceed() {
70 // Arrange
71 when(commentMapper.deleteById(1L)).thenReturn(1);
72
73 // Act
74 boolean result = commentService.deleteComment(1L);
75
76 // Assert
77 assertTrue(result);
78 verify(commentMapper).deleteById(1L);
79 }
80
81 @Test
82 @DisplayName("获取评论-没有评论")
83 void getCommentsByPostId_WhenNoComments_ShouldReturnEmptyList() {
84 // Arrange
85 when(commentMapper.findTopLevelCommentsByPostId(999L)).thenReturn(Arrays.asList());
86
87 // Act
88 List<Comment> result = commentService.getCommentsByPostId(999L);
89
90 // Assert
91 assertNotNull(result);
92 assertTrue(result.isEmpty());
93 verify(commentMapper).findTopLevelCommentsByPostId(999L);
94 }
95
96 @Test
97 @DisplayName("获取评论-根据父评论ID返回子评论")
98 void getReplies_ShouldReturnReplies() {
99 // Arrange
100 List<Comment> expectedReplies = Arrays.asList(testComment);
101 when(commentMapper.findRepliesByParentCommentId(1L)).thenReturn(expectedReplies);
102
103 // Act
104 List<Comment> result = commentService.getReplies(1L, 5);
105
106 // Assert
107 assertNotNull(result);
108 assertFalse(result.isEmpty());
109 assertEquals(testComment.getCommentId(), result.get(0).getCommentId());
110 verify(commentMapper).findRepliesByParentCommentId(1L);
111 }
112
113}