22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.controller.GroupController; |
| 4 | import com.example.myproject.entity.Group; |
| 5 | import com.example.myproject.entity.GroupComments; |
| 6 | import com.example.myproject.service.GroupService; |
| 7 | import com.example.myproject.utils.Result; |
| 8 | import org.junit.jupiter.api.BeforeEach; |
| 9 | import org.junit.jupiter.api.Test; |
| 10 | import org.mockito.InjectMocks; |
| 11 | import org.mockito.Mock; |
| 12 | import org.mockito.MockitoAnnotations; |
| 13 | import org.springframework.http.ResponseEntity; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | |
| 18 | import static org.mockito.Mockito.*; |
| 19 | import static org.junit.jupiter.api.Assertions.*; |
| 20 | |
| 21 | |
| 22 | class GroupControllerTest { |
| 23 | |
| 24 | @InjectMocks |
| 25 | private GroupController groupController; |
| 26 | |
| 27 | @Mock |
| 28 | private GroupService groupService; |
| 29 | |
| 30 | @BeforeEach |
| 31 | void setup() { |
| 32 | MockitoAnnotations.openMocks(this); |
| 33 | } |
| 34 | |
| 35 | // 测试创建小组 |
| 36 | |
| 37 | @Test |
| 38 | void createGroupTest() { |
| 39 | Group groupRequest = new Group(); |
| 40 | groupRequest.setGroupName("Test Group"); |
| 41 | groupRequest.setDescription("This is a test group"); |
| 42 | |
| 43 | // 模拟服务层的返回值 |
| 44 | Map<String, Object> mockResponse = new HashMap<>(); |
| 45 | mockResponse.put("group_id", 1L); |
| 46 | mockResponse.put("message", "兴趣小组创建成功"); |
| 47 | |
| 48 | ResponseEntity<Map<String, Object>> mockResponseEntity = ResponseEntity.ok(mockResponse); |
| 49 | when(groupService.createGroup(groupRequest)).thenReturn(mockResponseEntity); |
| 50 | |
| 51 | // 调用控制器的方法 |
| 52 | ResponseEntity<?> responseEntity = groupController.createGroup(groupRequest); |
| 53 | Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getBody(); |
| 54 | Result<Map<String, Object>> result = Result.success(resultMap, "兴趣小组创建成功"); |
| 55 | |
| 56 | // 验证返回的结果 |
| 57 | assertEquals("200", result.getCode()); |
| 58 | assertEquals("兴趣小组创建成功", result.getMsg()); |
| 59 | assertEquals(1L, result.getData().get("group_id")); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | // 测试加入小组接口 |
| 64 | @Test |
| 65 | void testJoinGroup() { |
| 66 | Long groupId = 1L; |
| 67 | Long userId = 1L; |
| 68 | |
| 69 | // 模拟服务层的返回值 |
| 70 | Map<String, Object> mockResponse = new HashMap<>(); |
| 71 | mockResponse.put("status", "success"); |
| 72 | mockResponse.put("message", "成功加入小组"); |
| 73 | when(groupService.joinGroup(groupId, userId)).thenReturn(ResponseEntity.ok(mockResponse)); |
| 74 | |
| 75 | // 调用控制器方法 |
| 76 | Map<String, Long> requestBody = Map.of("user_id", userId); |
| 77 | ResponseEntity<Map<String, Object>> response = groupController.joinGroup(groupId, requestBody); |
| 78 | |
| 79 | // 验证返回的结果 |
| 80 | assertEquals(200, response.getStatusCodeValue()); |
| 81 | assertEquals("success", response.getBody().get("status")); |
| 82 | assertEquals("成功加入小组", response.getBody().get("message")); |
| 83 | } |
| 84 | |
| 85 | // 测试退出小组接口 |
| 86 | @Test |
| 87 | void testLeaveGroup() { |
| 88 | Long groupId = 1L; |
| 89 | Long userId = 1L; |
| 90 | |
| 91 | // 模拟服务层的返回值 |
| 92 | Map<String, Object> mockResponse = new HashMap<>(); |
| 93 | mockResponse.put("status", "success"); |
| 94 | mockResponse.put("message", "成功退出小组"); |
| 95 | when(groupService.leaveGroup(groupId, userId)).thenReturn(ResponseEntity.ok(mockResponse)); |
| 96 | |
| 97 | // 调用控制器方法 |
| 98 | Map<String, Long> requestBody = Map.of("user_id", userId); |
| 99 | ResponseEntity<Map<String, Object>> response = groupController.leaveGroup(groupId, requestBody); |
| 100 | |
| 101 | // 验证返回的结果 |
| 102 | assertEquals(200, response.getStatusCodeValue()); |
| 103 | assertEquals("success", response.getBody().get("status")); |
| 104 | assertEquals("成功退出小组", response.getBody().get("message")); |
| 105 | } |
| 106 | |
| 107 | // 测试获取小组成员接口 |
| 108 | @Test |
| 109 | void testGetGroupMembers() { |
| 110 | Long groupId = 1L; |
| 111 | |
| 112 | // 模拟服务层的返回值 |
| 113 | Map<String, Object> mockResponse = new HashMap<>(); |
| 114 | mockResponse.put("status", "success"); |
| 115 | mockResponse.put("members", "mock_members"); |
| 116 | when(groupService.getGroupMembers(groupId)).thenReturn(ResponseEntity.ok(mockResponse)); |
| 117 | |
| 118 | // 调用控制器方法 |
| 119 | ResponseEntity<Map<String, Object>> response = groupController.getGroupMembers(groupId); |
| 120 | |
| 121 | // 验证返回的结果 |
| 122 | assertEquals(200, response.getStatusCodeValue()); |
| 123 | assertEquals("success", response.getBody().get("status")); |
| 124 | assertNotNull(response.getBody().get("members")); |
| 125 | } |
| 126 | |
| 127 | // 测试发布帖子 |
| 128 | @Test |
| 129 | void createPostTest() { |
| 130 | Long groupId = 1L; |
| 131 | Long userId = 2L; |
| 132 | String postContent = "Test post content"; |
| 133 | String title = "Test post title"; |
| 134 | |
| 135 | // 模拟服务层的返回值 |
| 136 | Map<String, Object> mockResponse = new HashMap<>(); |
| 137 | mockResponse.put("post_id", 1L); |
| 138 | mockResponse.put("message", "帖子创建成功"); |
| 139 | |
| 140 | when(groupService.createPost(groupId, userId, postContent, title, null)).thenReturn(mockResponse); |
| 141 | |
| 142 | // 调用控制器的方法 |
| 143 | Map<String, Object> result = groupController.createPost(groupId, userId, postContent, title, null).getBody(); |
| 144 | |
| 145 | // 验证返回的结果 |
| 146 | assertEquals("帖子创建成功", result.get("message")); |
| 147 | assertEquals(1L, result.get("post_id")); |
| 148 | } |
| 149 | |
| 150 | // 测试点赞帖子 |
| 151 | @Test |
| 152 | void likePostTest() { |
| 153 | Long postId = 1L; |
| 154 | |
| 155 | // 模拟服务层的返回值 |
| 156 | Map<String, Object> mockResponse = new HashMap<>(); |
| 157 | mockResponse.put("status", "success"); |
| 158 | mockResponse.put("message", "点赞成功"); |
| 159 | |
| 160 | when(groupService.likePost(postId)).thenReturn(mockResponse); |
| 161 | |
| 162 | // 调用控制器的方法 |
| 163 | Map<String, Object> resultMap = groupController.likePost(postId).getBody(); |
| 164 | |
| 165 | // 验证返回的结果 |
| 166 | assertEquals("点赞成功", resultMap.get("message")); |
| 167 | } |
| 168 | |
| 169 | // 测试取消点赞 |
| 170 | @Test |
| 171 | void unlikePostTest() { |
| 172 | Long postId = 1L; |
| 173 | |
| 174 | // 模拟服务层的返回值 |
| 175 | Map<String, Object> mockResponse = new HashMap<>(); |
| 176 | mockResponse.put("status", "success"); |
| 177 | mockResponse.put("message", "取消点赞成功"); |
| 178 | |
| 179 | when(groupService.unlikePost(postId)).thenReturn(mockResponse); |
| 180 | |
| 181 | // 调用控制器的方法 |
| 182 | Map<String, Object> resultMap = groupController.unlikePost(postId).getBody(); |
| 183 | |
| 184 | // 验证返回的结果 |
| 185 | assertEquals("取消点赞成功", resultMap.get("message")); |
| 186 | } |
| 187 | |
| 188 | // 测试添加评论 |
| 189 | @Test |
| 190 | void addCommentTest() { |
| 191 | Long postId = 1L; |
| 192 | GroupComments comment = new GroupComments(); |
| 193 | comment.setUserId(2L); |
| 194 | comment.setContent("This is a comment"); |
| 195 | |
| 196 | // 模拟服务层的返回值 |
| 197 | Map<String, Object> mockResponse = new HashMap<>(); |
| 198 | mockResponse.put("status", "success"); |
| 199 | mockResponse.put("message", "评论成功"); |
| 200 | |
| 201 | when(groupService.addComment(postId, comment)).thenReturn(mockResponse); |
| 202 | |
| 203 | // 调用控制器的方法 |
| 204 | Map<String, Object> resultMap = groupController.addComment(postId, comment).getBody(); |
| 205 | |
| 206 | // 验证返回的结果 |
| 207 | assertEquals("评论成功", resultMap.get("message")); |
| 208 | } |
| 209 | } |