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