blob: 51bc26b376bb914f8b05a5139e3ca719b5cf7cca [file] [log] [blame]
223010711f457dc2025-04-15 17:35:55 +08001package com.example.g8backend.service;
223010711f457dc2025-04-15 17:35:55 +08002import com.example.g8backend.entity.Post;
夜雨声烦f77d8132025-04-24 19:31:18 +08003import com.example.g8backend.mapper.*;
223010711f457dc2025-04-15 17:35:55 +08004import com.example.g8backend.service.impl.PostServiceImpl;
5import org.junit.jupiter.api.BeforeEach;
6import org.junit.jupiter.api.DisplayName;
7import org.junit.jupiter.api.Test;
8import org.junit.jupiter.api.extension.ExtendWith;
9import org.mockito.Mock;
10import org.mockito.MockitoAnnotations;
11import org.springframework.test.context.junit.jupiter.SpringExtension;
223010711f457dc2025-04-15 17:35:55 +080012import java.sql.Timestamp;
13import java.util.Arrays;
14import java.util.List;
223010711f457dc2025-04-15 17:35:55 +080015import static org.junit.jupiter.api.Assertions.*;
16import static org.mockito.ArgumentMatchers.any;
17import static org.mockito.Mockito.*;
223010711f457dc2025-04-15 17:35:55 +080018@ExtendWith(SpringExtension.class)
19@DisplayName("帖子服务测试")
20class PostServiceTest {
223010711f457dc2025-04-15 17:35:55 +080021 @Mock
22 private PostMapper postMapper;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080023 @Mock
24 private PostViewMapper postViewMapper;
夜雨声烦368e3562025-04-24 01:49:46 +080025 @Mock
26 private CommentMapper commentMapper;
223010711f457dc2025-04-15 17:35:55 +080027 private PostServiceImpl postService;
夜雨声烦f77d8132025-04-24 19:31:18 +080028 private UserTagPreferenceMapper userTagPreferenceMapper;
29 private PostTagMapper postTagMapper;
223010711f457dc2025-04-15 17:35:55 +080030 private Post testPost;
223010711f457dc2025-04-15 17:35:55 +080031 @BeforeEach
32 void setUp() {
33 MockitoAnnotations.openMocks(this);
夜雨声烦f77d8132025-04-24 19:31:18 +080034 postService = new PostServiceImpl(postMapper, postViewMapper,commentMapper,userTagPreferenceMapper,postTagMapper);
223010711f457dc2025-04-15 17:35:55 +080035 testPost = createTestPost();
36 }
223010711f457dc2025-04-15 17:35:55 +080037 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 }
223010711f457dc2025-04-15 17:35:55 +080046 @Test
47 @DisplayName("创建帖子-成功")
48 void save_ShouldSucceed() {
49 // Arrange
50 when(postMapper.insert(any(Post.class))).thenReturn(1);
223010711f457dc2025-04-15 17:35:55 +080051 // Act
52 boolean result = postService.save(testPost);
223010711f457dc2025-04-15 17:35:55 +080053 // Assert
54 assertTrue(result);
55 verify(postMapper).insert(testPost);
56 }
223010711f457dc2025-04-15 17:35:55 +080057 @Test
58 @DisplayName("获取帖子-通过ID存在")
59 void getById_WhenExists_ShouldReturnPost() {
60 // Arrange
61 when(postMapper.selectById(1L)).thenReturn(testPost);
223010711f457dc2025-04-15 17:35:55 +080062 // Act
63 Post result = postService.getById(1L);
223010711f457dc2025-04-15 17:35:55 +080064 // Assert
65 assertNotNull(result);
66 assertEquals(testPost.getPostId(), result.getPostId());
67 verify(postMapper).selectById(1L);
68 }
223010711f457dc2025-04-15 17:35:55 +080069 @Test
70 @DisplayName("获取帖子-通过ID不存在")
71 void getById_WhenNotExists_ShouldReturnNull() {
72 // Arrange
73 when(postMapper.selectById(999L)).thenReturn(null);
223010711f457dc2025-04-15 17:35:55 +080074 // Act
75 Post result = postService.getById(999L);
223010711f457dc2025-04-15 17:35:55 +080076 // Assert
77 assertNull(result);
78 verify(postMapper).selectById(999L);
79 }
223010711f457dc2025-04-15 17:35:55 +080080 @Test
81 @DisplayName("更新帖子-成功")
82 void updateById_ShouldSucceed() {
83 // Arrange
84 when(postMapper.updateById(any(Post.class))).thenReturn(1);
223010711f457dc2025-04-15 17:35:55 +080085 // Act
86 boolean result = postService.updateById(testPost);
223010711f457dc2025-04-15 17:35:55 +080087 // Assert
88 assertTrue(result);
89 verify(postMapper).updateById(testPost);
90 }
223010711f457dc2025-04-15 17:35:55 +080091 @Test
92 @DisplayName("删除帖子-成功")
93 void removeById_ShouldSucceed() {
94 // Arrange
95 when(postMapper.deleteById(1L)).thenReturn(1);
223010711f457dc2025-04-15 17:35:55 +080096 // Act
97 boolean result = postService.removeById(1L);
223010711f457dc2025-04-15 17:35:55 +080098 // Assert
99 assertTrue(result);
100 verify(postMapper).deleteById(1L);
101 }
223010711f457dc2025-04-15 17:35:55 +0800102 @Test
103 @DisplayName("获取用户帖子列表")
104 void getPostsByUserId_ShouldReturnPosts() {
105 // Arrange
106 List<Post> expectedPosts = Arrays.asList(testPost);
107 when(postMapper.getPostsByUserId(1L)).thenReturn(expectedPosts);
223010711f457dc2025-04-15 17:35:55 +0800108 // Act
109 List<Post> result = postService.getPostsByUserId(1L);
223010711f457dc2025-04-15 17:35:55 +0800110 // Assert
111 assertNotNull(result);
112 assertFalse(result.isEmpty());
113 assertEquals(testPost.getPostId(), result.get(0).getPostId());
114 verify(postMapper).getPostsByUserId(1L);
115 }
223010711f457dc2025-04-15 17:35:55 +0800116 @Test
117 @DisplayName("获取用户帖子-空列表")
118 void getPostsByUserId_WhenNoPosts_ShouldReturnEmptyList() {
119 // Arrange
120 when(postMapper.getPostsByUserId(999L)).thenReturn(Arrays.asList());
223010711f457dc2025-04-15 17:35:55 +0800121 // Act
122 List<Post> result = postService.getPostsByUserId(999L);
223010711f457dc2025-04-15 17:35:55 +0800123 // Assert
124 assertNotNull(result);
125 assertTrue(result.isEmpty());
126 verify(postMapper).getPostsByUserId(999L);
127 }
夜雨声烦4527a722025-04-23 17:04:25 +0800128 // 新增测试方法:搜索帖子,支持多个标签
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);
夜雨声烦4527a722025-04-23 17:04:25 +0800137 // 模拟PostMapper的searchPosts方法
138 when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
夜雨声烦4527a722025-04-23 17:04:25 +0800139 // Act
140 List<Post> result = postService.searchPosts(keyword, tagIds, author);
夜雨声烦4527a722025-04-23 17:04:25 +0800141 // Assert
142 assertNotNull(result);
143 assertFalse(result.isEmpty());
144 assertEquals(testPost.getPostId(), result.get(0).getPostId());
145 verify(postMapper).searchPosts(keyword, tagIds, author);
146 }
夜雨声烦4527a722025-04-23 17:04:25 +0800147 @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(); // 没有匹配的帖子
夜雨声烦4527a722025-04-23 17:04:25 +0800155 // 模拟PostMapper的searchPosts方法
156 when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
夜雨声烦4527a722025-04-23 17:04:25 +0800157 // Act
158 List<Post> result = postService.searchPosts(keyword, tagIds, author);
夜雨声烦4527a722025-04-23 17:04:25 +0800159 // Assert
160 assertNotNull(result);
161 assertTrue(result.isEmpty());
162 verify(postMapper).searchPosts(keyword, tagIds, author);
163 }
223010711f457dc2025-04-15 17:35:55 +0800164}