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