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