22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.controller.TaskController; |
| 4 | import com.example.myproject.service.TaskService; |
| 5 | import org.junit.jupiter.api.BeforeEach; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | import org.mockito.InjectMocks; |
| 8 | import org.mockito.Mock; |
| 9 | import org.mockito.MockitoAnnotations; |
| 10 | import org.springframework.http.ResponseEntity; |
| 11 | |
| 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 TaskControllerTest { |
| 19 | |
| 20 | @InjectMocks |
| 21 | private TaskController taskController; |
| 22 | |
| 23 | @Mock |
| 24 | private TaskService taskService; |
| 25 | |
| 26 | @BeforeEach |
| 27 | void setup() { |
| 28 | MockitoAnnotations.openMocks(this); |
| 29 | } |
| 30 | |
| 31 | // 测试获取当前用户的新手任务列表接口 |
| 32 | @Test |
| 33 | void testGetAllTasks() { |
| 34 | Long userId = 1L; |
| 35 | Map<String, Object> mockResponse = new HashMap<>(); |
| 36 | mockResponse.put("tasks", "mock_tasks"); |
| 37 | |
| 38 | // 模拟 taskService.getAllTasksForUser 返回 |
| 39 | when(taskService.getAllTasksForUser(userId)).thenReturn(mockResponse); |
| 40 | |
| 41 | // 创建请求体 |
| 42 | Map<String, Object> request = new HashMap<>(); |
| 43 | request.put("user_id", userId); |
| 44 | |
| 45 | // 调用控制器方法 |
| 46 | Map<String, Object> response = taskController.getAllTasks(request); |
| 47 | |
| 48 | // 验证返回的结果 |
| 49 | assertEquals("mock_tasks", response.get("tasks")); |
| 50 | } |
| 51 | |
| 52 | // 测试更新任务状态接口 |
| 53 | @Test |
| 54 | void testUpdateTaskStatus() { |
| 55 | Long userId = 1L; |
| 56 | String taskId = "task_123"; |
| 57 | Map<String, Object> mockResponse = new HashMap<>(); |
| 58 | mockResponse.put("status", "success"); |
| 59 | mockResponse.put("message", "任务状态已更新"); |
| 60 | |
| 61 | // 模拟 taskService.updateTaskStatus 返回 |
| 62 | when(taskService.updateTaskStatus(userId, taskId)).thenReturn(mockResponse); |
| 63 | |
| 64 | // 创建请求体 |
| 65 | Map<String, Object> request = new HashMap<>(); |
| 66 | request.put("user_id", userId); |
| 67 | request.put("task_id", taskId); |
| 68 | |
| 69 | // 调用控制器方法 |
| 70 | Map<String, Object> response = taskController.updateTaskStatus(request); |
| 71 | |
| 72 | // 验证返回的结果 |
| 73 | assertEquals("success", response.get("status")); |
| 74 | assertEquals("任务状态已更新", response.get("message")); |
| 75 | } |
| 76 | |
| 77 | // 测试获取当前经验和任务奖励接口 |
| 78 | @Test |
| 79 | void testGetExperience() { |
| 80 | Long userId = 1L; |
| 81 | Map<String, Object> mockResponse = new HashMap<>(); |
| 82 | mockResponse.put("current_experience", 1500); |
| 83 | mockResponse.put("level", "Intermediate"); |
| 84 | mockResponse.put("reward", Map.of("experience", 1000, "points", 200)); |
| 85 | |
| 86 | // 模拟 taskService.getUserExperience 返回 |
| 87 | when(taskService.getUserExperience(userId)).thenReturn(mockResponse); |
| 88 | |
| 89 | // 创建请求体 |
| 90 | Map<String, Object> request = new HashMap<>(); |
| 91 | request.put("user_id", userId); |
| 92 | |
| 93 | // 调用控制器方法 |
| 94 | Map<String, Object> response = taskController.getExperience(request); |
| 95 | |
| 96 | // 验证返回的结果 |
| 97 | assertEquals(1500, response.get("current_experience")); |
| 98 | assertEquals("Intermediate", response.get("level")); |
| 99 | assertTrue(response.containsKey("reward")); |
| 100 | } |
| 101 | |
| 102 | // 测试获取当前的指引步骤接口 |
| 103 | @Test |
| 104 | void testGetNewStep() { |
| 105 | Long userId = 1L; |
| 106 | Map<String, Object> mockResponse = new HashMap<>(); |
| 107 | mockResponse.put("current_step", "step_1"); |
| 108 | mockResponse.put("total_steps", 5); |
| 109 | mockResponse.put("step_description", "Complete the introduction task"); |
| 110 | |
| 111 | // 模拟 taskService.getNewStep 返回 |
| 112 | when(taskService.getNewStep(userId)).thenReturn(mockResponse); |
| 113 | |
| 114 | // 创建请求体 |
| 115 | Map<String, Object> request = new HashMap<>(); |
| 116 | request.put("user_id", userId); |
| 117 | |
| 118 | // 调用控制器方法 |
| 119 | Map<String, Object> response = taskController.getNewStep(request); |
| 120 | |
| 121 | // 验证返回的结果 |
| 122 | assertEquals("step_1", response.get("current_step")); |
| 123 | assertEquals(5, response.get("total_steps")); |
| 124 | assertEquals("Complete the introduction task", response.get("step_description")); |
| 125 | } |
| 126 | |
| 127 | // 测试更新进度接口 |
| 128 | @Test |
| 129 | void testUpdateProgress() { |
| 130 | Long userId = 1L; |
| 131 | String taskId = "task_123"; |
| 132 | Integer progress = 50; |
| 133 | Map<String, Object> mockResponse = new HashMap<>(); |
| 134 | mockResponse.put("status", "success"); |
| 135 | mockResponse.put("message", "进度已更新"); |
| 136 | |
| 137 | // 模拟 taskService.updateTaskProgress 返回 |
| 138 | when(taskService.updateTaskProgress(userId, taskId, progress)).thenReturn(mockResponse); |
| 139 | |
| 140 | // 创建请求体 |
| 141 | Map<String, Object> request = new HashMap<>(); |
| 142 | request.put("user_id", userId); |
| 143 | request.put("task_id", taskId); |
| 144 | request.put("progress", progress); |
| 145 | |
| 146 | // 调用控制器方法 |
| 147 | Map<String, Object> response = taskController.updateProgress(request); |
| 148 | |
| 149 | // 验证返回的结果 |
| 150 | assertEquals("success", response.get("status")); |
| 151 | assertEquals("进度已更新", response.get("message")); |
| 152 | } |
| 153 | |
| 154 | // 测试领取任务奖励接口 |
| 155 | @Test |
| 156 | void testRewardClaim() { |
| 157 | Long userId = 1L; |
| 158 | String taskId = "task_123"; |
| 159 | Map<String, Object> mockResponse = new HashMap<>(); |
| 160 | mockResponse.put("status", "success"); |
| 161 | mockResponse.put("message", "奖励已领取"); |
| 162 | mockResponse.put("reward", Map.of("experience", 1000, "points", 200)); |
| 163 | |
| 164 | // 模拟 taskService.claimReward 返回 |
| 165 | when(taskService.claimReward(userId, taskId)).thenReturn(mockResponse); |
| 166 | |
| 167 | // 创建请求体 |
| 168 | Map<String, Object> request = new HashMap<>(); |
| 169 | request.put("user_id", userId); |
| 170 | request.put("task_id", taskId); |
| 171 | |
| 172 | // 调用控制器方法 |
| 173 | Map<String, Object> response = taskController.rewardClaim(request); |
| 174 | |
| 175 | // 验证返回的结果 |
| 176 | assertEquals("success", response.get("status")); |
| 177 | assertEquals("奖励已领取", response.get("message")); |
| 178 | assertTrue(response.containsKey("reward")); |
| 179 | } |
| 180 | |
| 181 | // 测试检查任务奖励状态接口 |
| 182 | @Test |
| 183 | void testRewardReview() { |
| 184 | Long userId = 1L; |
| 185 | String taskId = "task_123"; |
| 186 | Map<String, Object> mockResponse = new HashMap<>(); |
| 187 | mockResponse.put("status", "reward_not_issued"); |
| 188 | mockResponse.put("message", "任务奖励未被领取"); |
| 189 | |
| 190 | // 模拟 taskService.checkRewardStatus 返回 |
| 191 | when(taskService.checkRewardStatus(userId, taskId)).thenReturn(mockResponse); |
| 192 | |
| 193 | // 创建请求体 |
| 194 | Map<String, Object> request = new HashMap<>(); |
| 195 | request.put("user_id", userId); |
| 196 | request.put("task_id", taskId); |
| 197 | |
| 198 | // 调用控制器方法 |
| 199 | Map<String, Object> response = taskController.rewardReview(request); |
| 200 | |
| 201 | // 验证返回的结果 |
| 202 | assertEquals("reward_not_issued", response.get("status")); |
| 203 | assertEquals("任务奖励未被领取", response.get("message")); |
| 204 | } |
| 205 | } |