blob: 9f8fb14dd3db2fc5043574564d3babb6be1f2ddd [file] [log] [blame]
223010711f457dc2025-04-15 17:35:55 +08001package com.example.g8backend.service;
2
3import com.example.g8backend.entity.Post;
4import com.example.g8backend.mapper.PostMapper;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08005import com.example.g8backend.mapper.PostViewMapper;
223010711f457dc2025-04-15 17:35:55 +08006import com.example.g8backend.service.impl.PostServiceImpl;
7import org.junit.jupiter.api.BeforeEach;
8import org.junit.jupiter.api.DisplayName;
9import org.junit.jupiter.api.Test;
10import org.junit.jupiter.api.extension.ExtendWith;
11import org.mockito.Mock;
12import org.mockito.MockitoAnnotations;
13import org.springframework.test.context.junit.jupiter.SpringExtension;
14
15import java.sql.Timestamp;
16import java.util.Arrays;
17import java.util.List;
18
19import static org.junit.jupiter.api.Assertions.*;
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.Mockito.*;
22
23@ExtendWith(SpringExtension.class)
24@DisplayName("帖子服务测试")
25class PostServiceTest {
26
27 @Mock
28 private PostMapper postMapper;
29
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080030 @Mock
31 private PostViewMapper postViewMapper;
32
223010711f457dc2025-04-15 17:35:55 +080033 private PostServiceImpl postService;
34
35 private Post testPost;
36
37 @BeforeEach
38 void setUp() {
39 MockitoAnnotations.openMocks(this);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080040 postService = new PostServiceImpl(postMapper, postViewMapper);
223010711f457dc2025-04-15 17:35:55 +080041 testPost = createTestPost();
42 }
43
44 private Post createTestPost() {
45 Post post = new Post();
46 post.setPostId(1L);
47 post.setUserId(1L);
48 post.setPostTitle("测试标题");
49 post.setPostContent("测试内容");
50 post.setCreatedAt(new Timestamp(System.currentTimeMillis()));
51 return post;
52 }
53
54 @Test
55 @DisplayName("创建帖子-成功")
56 void save_ShouldSucceed() {
57 // Arrange
58 when(postMapper.insert(any(Post.class))).thenReturn(1);
59
60 // Act
61 boolean result = postService.save(testPost);
62
63 // Assert
64 assertTrue(result);
65 verify(postMapper).insert(testPost);
66 }
67
68 @Test
69 @DisplayName("获取帖子-通过ID存在")
70 void getById_WhenExists_ShouldReturnPost() {
71 // Arrange
72 when(postMapper.selectById(1L)).thenReturn(testPost);
73
74 // Act
75 Post result = postService.getById(1L);
76
77 // Assert
78 assertNotNull(result);
79 assertEquals(testPost.getPostId(), result.getPostId());
80 verify(postMapper).selectById(1L);
81 }
82
83 @Test
84 @DisplayName("获取帖子-通过ID不存在")
85 void getById_WhenNotExists_ShouldReturnNull() {
86 // Arrange
87 when(postMapper.selectById(999L)).thenReturn(null);
88
89 // Act
90 Post result = postService.getById(999L);
91
92 // Assert
93 assertNull(result);
94 verify(postMapper).selectById(999L);
95 }
96
97 @Test
98 @DisplayName("更新帖子-成功")
99 void updateById_ShouldSucceed() {
100 // Arrange
101 when(postMapper.updateById(any(Post.class))).thenReturn(1);
102
103 // Act
104 boolean result = postService.updateById(testPost);
105
106 // Assert
107 assertTrue(result);
108 verify(postMapper).updateById(testPost);
109 }
110
111 @Test
112 @DisplayName("删除帖子-成功")
113 void removeById_ShouldSucceed() {
114 // Arrange
115 when(postMapper.deleteById(1L)).thenReturn(1);
116
117 // Act
118 boolean result = postService.removeById(1L);
119
120 // Assert
121 assertTrue(result);
122 verify(postMapper).deleteById(1L);
123 }
124
125 @Test
126 @DisplayName("获取用户帖子列表")
127 void getPostsByUserId_ShouldReturnPosts() {
128 // Arrange
129 List<Post> expectedPosts = Arrays.asList(testPost);
130 when(postMapper.getPostsByUserId(1L)).thenReturn(expectedPosts);
131
132 // Act
133 List<Post> result = postService.getPostsByUserId(1L);
134
135 // Assert
136 assertNotNull(result);
137 assertFalse(result.isEmpty());
138 assertEquals(testPost.getPostId(), result.get(0).getPostId());
139 verify(postMapper).getPostsByUserId(1L);
140 }
141
142 @Test
143 @DisplayName("获取用户帖子-空列表")
144 void getPostsByUserId_WhenNoPosts_ShouldReturnEmptyList() {
145 // Arrange
146 when(postMapper.getPostsByUserId(999L)).thenReturn(Arrays.asList());
147
148 // Act
149 List<Post> result = postService.getPostsByUserId(999L);
150
151 // Assert
152 assertNotNull(result);
153 assertTrue(result.isEmpty());
154 verify(postMapper).getPostsByUserId(999L);
155 }
夜雨声烦4527a722025-04-23 17:04:25 +0800156
157 // 新增测试方法:搜索帖子,支持多个标签
158 @Test
159 @DisplayName("搜索帖子-通过关键词和多个标签")
160 void searchPosts_WithKeywordAndTags_ShouldReturnPosts() {
161 // Arrange
162 List<Long> tagIds = Arrays.asList(1L, 2L); // 假设存在标签ID 1和2
163 String keyword = "测试内容";
164 String author = "作者";
165 List<Post> expectedPosts = Arrays.asList(testPost);
166
167 // 模拟PostMapper的searchPosts方法
168 when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
169
170 // Act
171 List<Post> result = postService.searchPosts(keyword, tagIds, author);
172
173 // Assert
174 assertNotNull(result);
175 assertFalse(result.isEmpty());
176 assertEquals(testPost.getPostId(), result.get(0).getPostId());
177 verify(postMapper).searchPosts(keyword, tagIds, author);
178 }
179
180 @Test
181 @DisplayName("搜索帖子-没有匹配的帖子")
182 void searchPosts_WhenNoPosts_ShouldReturnEmptyList() {
183 // Arrange
184 List<Long> tagIds = Arrays.asList(3L, 4L); // 假设标签ID 3和4没有匹配的帖子
185 String keyword = "不存在的内容";
186 String author = "不存在的作者";
187 List<Post> expectedPosts = Arrays.asList(); // 没有匹配的帖子
188
189 // 模拟PostMapper的searchPosts方法
190 when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
191
192 // Act
193 List<Post> result = postService.searchPosts(keyword, tagIds, author);
194
195 // Assert
196 assertNotNull(result);
197 assertTrue(result.isEmpty());
198 verify(postMapper).searchPosts(keyword, tagIds, author);
199 }
200
223010711f457dc2025-04-15 17:35:55 +0800201}