22301138 | 1c35910 | 2025-06-03 15:19:59 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.Collections; |
| 4 | import com.example.myproject.entity.Post; |
| 5 | import com.example.myproject.entity.Users; |
| 6 | import com.example.myproject.repository.CollectionsRepository; |
| 7 | import com.example.myproject.repository.PostRepository; |
| 8 | import com.example.myproject.repository.UserRepository; |
| 9 | import com.example.myproject.service.PostService; |
| 10 | import com.example.myproject.utils.Result; |
| 11 | import org.junit.jupiter.api.BeforeEach; |
| 12 | import org.junit.jupiter.api.Test; |
| 13 | import org.mockito.*; |
| 14 | import org.springframework.web.multipart.MultipartFile; |
| 15 | |
| 16 | import java.util.*; |
| 17 | |
| 18 | import static org.mockito.Mockito.*; |
| 19 | import static org.junit.jupiter.api.Assertions.*; |
| 20 | |
| 21 | class PostControllerTest { |
| 22 | |
| 23 | @InjectMocks |
| 24 | private PostController postController; |
| 25 | |
| 26 | @Mock |
| 27 | private PostService postService; |
| 28 | |
| 29 | @Mock |
| 30 | private CollectionsRepository collectionsRepository; |
| 31 | |
| 32 | @Mock |
| 33 | private PostRepository postRepository; |
| 34 | |
| 35 | @Mock |
| 36 | private UserRepository userRepository; |
| 37 | |
| 38 | @InjectMocks |
| 39 | private UserController userController; |
| 40 | |
| 41 | @BeforeEach |
| 42 | void setup() { |
| 43 | MockitoAnnotations.openMocks(this); |
| 44 | } |
| 45 | |
| 46 | // 测试创建帖子 |
| 47 | void createPostTest() { |
| 48 | Long userId = 1L; |
| 49 | String postContent = "Test Post Content"; |
| 50 | String title = "Test Post Title"; |
| 51 | MultipartFile[] imageFiles = new MultipartFile[0]; // Empty array for simplicity |
| 52 | |
| 53 | // 模拟服务层的返回值 |
| 54 | Map<String, Object> responseMap = new HashMap<>(); |
| 55 | responseMap.put("postNo", 123L); |
| 56 | responseMap.put("message", "帖子创建成功"); |
| 57 | |
| 58 | when(postService.createPost(userId, postContent, title, imageFiles)).thenReturn(responseMap); |
| 59 | |
| 60 | // 调用控制器的方法 |
| 61 | Map<String, Object> resultMap = postController.createPost(userId, postContent, title, imageFiles); |
| 62 | |
| 63 | // 手动将 Map<String, Object> 包装到 Result.success() |
| 64 | Result<Map<String, Object>> result = Result.success(resultMap, "帖子创建成功"); |
| 65 | |
| 66 | // 验证返回的结果 |
| 67 | assertEquals("200", result.getCode()); |
| 68 | assertEquals("帖子创建成功", result.getMsg()); |
| 69 | assertEquals(123L, result.getData().get("postNo")); |
| 70 | } |
| 71 | |
| 72 | // 测试编辑帖子 |
| 73 | @Test |
| 74 | void updatePostTest() { |
| 75 | Long postId = 1L; |
| 76 | Post post = new Post(); |
| 77 | post.setPostNo(postId); |
| 78 | post.setTitle("Updated Title"); |
| 79 | post.setPostContent("Updated Content"); |
| 80 | |
| 81 | // 模拟服务层的行为 |
| 82 | doNothing().when(postService).updatePost(postId, post); |
| 83 | |
| 84 | // 调用控制器的方法 |
| 85 | String result = postController.updatePost(postId, post); |
| 86 | |
| 87 | // 验证返回的结果 |
| 88 | assertEquals("Post updated successfully!", result); |
| 89 | } |
| 90 | |
| 91 | // 测试删除帖子 |
| 92 | @Test |
| 93 | void deletePostTest() { |
| 94 | Long postId = 1L; |
| 95 | |
| 96 | // 模拟服务层的行为 |
| 97 | doNothing().when(postService).deletePost(postId); |
| 98 | |
| 99 | // 调用控制器的方法 |
| 100 | String result = postController.deletePost(postId); |
| 101 | |
| 102 | // 验证返回的结果 |
| 103 | assertEquals("Post deleted successfully!", result); |
| 104 | } |
| 105 | |
| 106 | // 测试点赞帖子 |
| 107 | @Test |
| 108 | void likePostTest() { |
| 109 | Long postId = 1L; |
| 110 | Long userId = 1L; |
| 111 | |
| 112 | // 模拟服务层的行为 |
| 113 | doNothing().when(postService).likePost(postId, userId); |
| 114 | |
| 115 | // 创建请求体 |
| 116 | Map<String, Long> requestBody = new HashMap<>(); |
| 117 | requestBody.put("user_id", userId); |
| 118 | |
| 119 | // 调用控制器的方法 |
| 120 | String result = postController.likePost(postId, requestBody); |
| 121 | |
| 122 | // 验证返回的结果 |
| 123 | assertEquals("Post liked successfully!", result); |
| 124 | } |
| 125 | |
| 126 | // 测试取消点赞帖子 |
| 127 | @Test |
| 128 | void unlikePostTest() { |
| 129 | Long postId = 1L; |
| 130 | Long userId = 1L; |
| 131 | |
| 132 | // 模拟服务层的行为 |
| 133 | doNothing().when(postService).unlikePost(postId, userId); |
| 134 | |
| 135 | // 创建请求体 |
| 136 | Map<String, Long> requestBody = new HashMap<>(); |
| 137 | requestBody.put("user_id", userId); |
| 138 | |
| 139 | // 调用控制器的方法 |
| 140 | String result = postController.unlikePost(postId, requestBody); |
| 141 | |
| 142 | // 验证返回的结果 |
| 143 | assertEquals("Post unliked successfully!", result); |
| 144 | } |
| 145 | |
| 146 | // 测试收藏帖子 |
| 147 | @Test |
| 148 | void collectPostTest() { |
| 149 | Long postId = 1L; |
| 150 | Long userId = 1L; |
| 151 | |
| 152 | // 模拟服务层的行为 |
| 153 | doNothing().when(postService).collectPost(postId, userId); |
| 154 | |
| 155 | // 创建请求体 |
| 156 | Map<String, Long> requestBody = new HashMap<>(); |
| 157 | requestBody.put("user_id", userId); |
| 158 | |
| 159 | // 调用控制器的方法 |
| 160 | String result = postController.collectPost(postId, requestBody); |
| 161 | |
| 162 | // 验证返回的结果 |
| 163 | assertEquals("Post collected successfully!", result); |
| 164 | } |
| 165 | |
| 166 | // 测试取消收藏帖子 |
| 167 | @Test |
| 168 | void uncollectPostTest() { |
| 169 | Long postId = 1L; |
| 170 | Long userId = 1L; |
| 171 | |
| 172 | // 模拟服务层的行为 |
| 173 | doNothing().when(postService).uncollectPost(postId, userId); |
| 174 | |
| 175 | // 创建请求体 |
| 176 | Map<String, Long> requestBody = new HashMap<>(); |
| 177 | requestBody.put("user_id", userId); |
| 178 | |
| 179 | // 调用控制器的方法 |
| 180 | String result = postController.uncollectPost(postId, requestBody); |
| 181 | |
| 182 | // 验证返回的结果 |
| 183 | assertEquals("Post uncollected successfully!", result); |
| 184 | } |
| 185 | |
| 186 | // 测试获取所有帖子 |
| 187 | @Test |
| 188 | void getAllPostsTest() { |
| 189 | // 模拟服务层的行为 |
| 190 | Map<String, Object> responseMap = new HashMap<>(); |
| 191 | responseMap.put("total", 5); |
| 192 | responseMap.put("posts", new ArrayList<>()); |
| 193 | |
| 194 | when(postService.getAllPosts()).thenReturn(responseMap); |
| 195 | |
| 196 | // 调用控制器的方法 |
| 197 | Map<String, Object> result = postController.getAllPosts(); |
| 198 | |
| 199 | // 验证返回的结果 |
| 200 | assertEquals(5, result.get("total")); |
| 201 | } |
| 202 | |
| 203 | // 测试根据帖子ID获取帖子 |
| 204 | @Test |
| 205 | void getPostByIdTest() { |
| 206 | Long postId = 1L; |
| 207 | Map<String, Object> responseMap = new HashMap<>(); |
| 208 | responseMap.put("postNo", 123L); |
| 209 | responseMap.put("message", "Post details"); |
| 210 | |
| 211 | // 模拟服务层的行为 |
| 212 | when(postService.getPostById(postId)).thenReturn(responseMap); |
| 213 | |
| 214 | // 调用控制器的方法 |
| 215 | Map<String, Object> result = postController.getPostById(postId); |
| 216 | |
| 217 | // 验证返回的结果 |
| 218 | assertEquals("Post details", result.get("message")); |
| 219 | assertEquals(123L, result.get("postNo")); |
| 220 | } |
| 221 | |
| 222 | // 测试获取用户收藏的所有帖子接口 |
| 223 | @Test |
| 224 | void testGetAllCollections() { |
| 225 | Long userId = 1L; |
| 226 | |
| 227 | // 模拟用户收藏的数据 |
| 228 | Collections collection = new Collections(); |
| 229 | collection.setCollectionId(1L); |
| 230 | collection.setUserId(userId); |
| 231 | collection.setPostNo(101L); |
| 232 | |
| 233 | List<Collections> collections = new ArrayList<>(); |
| 234 | collections.add(collection); |
| 235 | |
| 236 | // 模拟帖子数据 |
| 237 | Post post = new Post(); |
| 238 | post.setPostNo(101L); |
| 239 | post.setTitle("Test Post Title"); |
| 240 | post.setPostContent("Test Post Content"); |
| 241 | post.setImageUrl("https://example.com/image.jpg"); |
| 242 | post.setUser_id(2L); // 作者用户ID |
| 243 | |
| 244 | Users user = new Users(); |
| 245 | user.setUserId(2L); |
| 246 | user.setUsername("testUser"); |
| 247 | user.setAvatarUrl("https://example.com/avatar.jpg"); |
| 248 | |
| 249 | // 设置模拟服务层的返回 |
| 250 | when(postService.getAllCollections(userId)).thenReturn(Arrays.asList( |
| 251 | Map.of( |
| 252 | "postNo", post.getPostNo(), |
| 253 | "title", post.getTitle(), |
| 254 | "postContent", post.getPostContent(), |
| 255 | "imageUrl", post.getImageUrl(), |
| 256 | "userId", post.getUser_id(), |
| 257 | "username", user.getUsername(), |
| 258 | "avatarUrl", user.getAvatarUrl() |
| 259 | ) |
| 260 | )); |
| 261 | |
| 262 | // 调用控制器方法 |
| 263 | List<Map<String, Object>> result = postController.getAllCollections(userId); |
| 264 | |
| 265 | // 验证返回的结果 |
| 266 | assertNotNull(result); |
| 267 | assertEquals(1, result.size()); // 验证返回数据的数量 |
| 268 | |
| 269 | // 验证返回的数据结构 |
| 270 | Map<String, Object> postInfo = result.get(0); |
| 271 | assertEquals(101L, postInfo.get("postNo")); |
| 272 | assertEquals("Test Post Title", postInfo.get("title")); |
| 273 | assertEquals("Test Post Content", postInfo.get("postContent")); |
| 274 | assertEquals("https://example.com/image.jpg", postInfo.get("imageUrl")); |
| 275 | assertEquals(2L, postInfo.get("userId")); |
| 276 | assertEquals("testUser", postInfo.get("username")); |
| 277 | assertEquals("https://example.com/avatar.jpg", postInfo.get("avatarUrl")); |
| 278 | } |
| 279 | } |