22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.Post; |
| 4 | import com.example.myproject.service.PostService; |
| 5 | import com.example.myproject.utils.Result; |
| 6 | import org.junit.jupiter.api.BeforeEach; |
| 7 | import org.junit.jupiter.api.Test; |
| 8 | import org.mockito.*; |
| 9 | import org.springframework.web.multipart.MultipartFile; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.HashMap; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | import static org.mockito.Mockito.*; |
| 16 | import static org.junit.jupiter.api.Assertions.*; |
| 17 | |
| 18 | class PostControllerTest { |
| 19 | |
| 20 | @InjectMocks |
| 21 | private PostController postController; |
| 22 | |
| 23 | @Mock |
| 24 | private PostService postService; |
| 25 | |
| 26 | @BeforeEach |
| 27 | void setup() { |
| 28 | MockitoAnnotations.openMocks(this); |
| 29 | } |
| 30 | |
| 31 | // 测试创建帖子 |
| 32 | void createPostTest() { |
| 33 | Long userId = 1L; |
| 34 | String postContent = "Test Post Content"; |
| 35 | String title = "Test Post Title"; |
| 36 | MultipartFile[] imageFiles = new MultipartFile[0]; // Empty array for simplicity |
| 37 | |
| 38 | // 模拟服务层的返回值 |
| 39 | Map<String, Object> responseMap = new HashMap<>(); |
| 40 | responseMap.put("postNo", 123L); |
| 41 | responseMap.put("message", "帖子创建成功"); |
| 42 | |
| 43 | when(postService.createPost(userId, postContent, title, imageFiles)).thenReturn(responseMap); |
| 44 | |
| 45 | // 调用控制器的方法 |
| 46 | Map<String, Object> resultMap = postController.createPost(userId, postContent, title, imageFiles); |
| 47 | |
| 48 | // 手动将 Map<String, Object> 包装到 Result.success() |
| 49 | Result<Map<String, Object>> result = Result.success(resultMap, "帖子创建成功"); |
| 50 | |
| 51 | // 验证返回的结果 |
| 52 | assertEquals("200", result.getCode()); |
| 53 | assertEquals("帖子创建成功", result.getMsg()); |
| 54 | assertEquals(123L, result.getData().get("postNo")); |
| 55 | } |
| 56 | |
| 57 | // 测试编辑帖子 |
| 58 | @Test |
| 59 | void updatePostTest() { |
| 60 | Long postId = 1L; |
| 61 | Post post = new Post(); |
| 62 | post.setPostNo(postId); |
| 63 | post.setTitle("Updated Title"); |
| 64 | post.setPostContent("Updated Content"); |
| 65 | |
| 66 | // 模拟服务层的行为 |
| 67 | doNothing().when(postService).updatePost(postId, post); |
| 68 | |
| 69 | // 调用控制器的方法 |
| 70 | String result = postController.updatePost(postId, post); |
| 71 | |
| 72 | // 验证返回的结果 |
| 73 | assertEquals("Post updated successfully!", result); |
| 74 | } |
| 75 | |
| 76 | // 测试删除帖子 |
| 77 | @Test |
| 78 | void deletePostTest() { |
| 79 | Long postId = 1L; |
| 80 | |
| 81 | // 模拟服务层的行为 |
| 82 | doNothing().when(postService).deletePost(postId); |
| 83 | |
| 84 | // 调用控制器的方法 |
| 85 | String result = postController.deletePost(postId); |
| 86 | |
| 87 | // 验证返回的结果 |
| 88 | assertEquals("Post deleted successfully!", result); |
| 89 | } |
| 90 | |
| 91 | // 测试点赞帖子 |
| 92 | @Test |
| 93 | void likePostTest() { |
| 94 | Long postId = 1L; |
| 95 | Long userId = 1L; |
| 96 | |
| 97 | // 模拟服务层的行为 |
| 98 | doNothing().when(postService).likePost(postId, userId); |
| 99 | |
| 100 | // 创建请求体 |
| 101 | Map<String, Long> requestBody = new HashMap<>(); |
| 102 | requestBody.put("user_id", userId); |
| 103 | |
| 104 | // 调用控制器的方法 |
| 105 | String result = postController.likePost(postId, requestBody); |
| 106 | |
| 107 | // 验证返回的结果 |
| 108 | assertEquals("Post liked successfully!", result); |
| 109 | } |
| 110 | |
| 111 | // 测试取消点赞帖子 |
| 112 | @Test |
| 113 | void unlikePostTest() { |
| 114 | Long postId = 1L; |
| 115 | Long userId = 1L; |
| 116 | |
| 117 | // 模拟服务层的行为 |
| 118 | doNothing().when(postService).unlikePost(postId, userId); |
| 119 | |
| 120 | // 创建请求体 |
| 121 | Map<String, Long> requestBody = new HashMap<>(); |
| 122 | requestBody.put("user_id", userId); |
| 123 | |
| 124 | // 调用控制器的方法 |
| 125 | String result = postController.unlikePost(postId, requestBody); |
| 126 | |
| 127 | // 验证返回的结果 |
| 128 | assertEquals("Post unliked successfully!", result); |
| 129 | } |
| 130 | |
| 131 | // 测试收藏帖子 |
| 132 | @Test |
| 133 | void collectPostTest() { |
| 134 | Long postId = 1L; |
| 135 | Long userId = 1L; |
| 136 | |
| 137 | // 模拟服务层的行为 |
| 138 | doNothing().when(postService).collectPost(postId, userId); |
| 139 | |
| 140 | // 创建请求体 |
| 141 | Map<String, Long> requestBody = new HashMap<>(); |
| 142 | requestBody.put("user_id", userId); |
| 143 | |
| 144 | // 调用控制器的方法 |
| 145 | String result = postController.collectPost(postId, requestBody); |
| 146 | |
| 147 | // 验证返回的结果 |
| 148 | assertEquals("Post collected successfully!", result); |
| 149 | } |
| 150 | |
| 151 | // 测试取消收藏帖子 |
| 152 | @Test |
| 153 | void uncollectPostTest() { |
| 154 | Long postId = 1L; |
| 155 | Long userId = 1L; |
| 156 | |
| 157 | // 模拟服务层的行为 |
| 158 | doNothing().when(postService).uncollectPost(postId, userId); |
| 159 | |
| 160 | // 创建请求体 |
| 161 | Map<String, Long> requestBody = new HashMap<>(); |
| 162 | requestBody.put("user_id", userId); |
| 163 | |
| 164 | // 调用控制器的方法 |
| 165 | String result = postController.uncollectPost(postId, requestBody); |
| 166 | |
| 167 | // 验证返回的结果 |
| 168 | assertEquals("Post uncollected successfully!", result); |
| 169 | } |
| 170 | |
| 171 | // 测试获取所有帖子 |
| 172 | @Test |
| 173 | void getAllPostsTest() { |
| 174 | // 模拟服务层的行为 |
| 175 | Map<String, Object> responseMap = new HashMap<>(); |
| 176 | responseMap.put("total", 5); |
| 177 | responseMap.put("posts", new ArrayList<>()); |
| 178 | |
| 179 | when(postService.getAllPosts()).thenReturn(responseMap); |
| 180 | |
| 181 | // 调用控制器的方法 |
| 182 | Map<String, Object> result = postController.getAllPosts(); |
| 183 | |
| 184 | // 验证返回的结果 |
| 185 | assertEquals(5, result.get("total")); |
| 186 | } |
| 187 | |
| 188 | // 测试根据帖子ID获取帖子 |
| 189 | @Test |
| 190 | void getPostByIdTest() { |
| 191 | Long postId = 1L; |
| 192 | Map<String, Object> responseMap = new HashMap<>(); |
| 193 | responseMap.put("postNo", 123L); |
| 194 | responseMap.put("message", "Post details"); |
| 195 | |
| 196 | // 模拟服务层的行为 |
| 197 | when(postService.getPostById(postId)).thenReturn(responseMap); |
| 198 | |
| 199 | // 调用控制器的方法 |
| 200 | Map<String, Object> result = postController.getPostById(postId); |
| 201 | |
| 202 | // 验证返回的结果 |
| 203 | assertEquals("Post details", result.get("message")); |
| 204 | assertEquals(123L, result.get("postNo")); |
| 205 | } |
| 206 | } |