22301138 | 1c35910 | 2025-06-03 15:19:59 +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 | // 调用控制器方法,改为通过请求参数传递 user_id |
| 42 | Map<String, Object> response = taskController.getAllTasks(userId); |
| 43 | |
| 44 | // 验证返回的结果 |
| 45 | assertEquals("mock_tasks", response.get("tasks")); |
| 46 | } |
| 47 | |
| 48 | // 测试更新任务状态接口 |
| 49 | @Test |
| 50 | void testUpdateTaskStatus() { |
| 51 | Long userId = 1L; |
| 52 | String taskId = "task_123"; |
| 53 | Map<String, Object> mockResponse = new HashMap<>(); |
| 54 | mockResponse.put("status", "success"); |
| 55 | mockResponse.put("message", "任务状态已更新"); |
| 56 | |
| 57 | // 模拟 taskService.updateTaskStatus 返回 |
| 58 | when(taskService.updateTaskStatus(userId, taskId)).thenReturn(mockResponse); |
| 59 | |
| 60 | // 创建请求体 |
| 61 | Map<String, Object> request = new HashMap<>(); |
| 62 | request.put("user_id", userId); |
| 63 | request.put("task_id", taskId); |
| 64 | |
| 65 | // 调用控制器方法 |
| 66 | Map<String, Object> response = taskController.updateTaskStatus(request); |
| 67 | |
| 68 | // 验证返回的结果 |
| 69 | assertEquals("success", response.get("status")); |
| 70 | assertEquals("任务状态已更新", response.get("message")); |
| 71 | } |
| 72 | |
| 73 | // 测试获取当前经验和任务奖励接口 |
| 74 | @Test |
| 75 | void testGetExperience() { |
| 76 | Long userId = 1L; |
| 77 | Map<String, Object> mockResponse = new HashMap<>(); |
| 78 | mockResponse.put("current_experience", 1500); |
| 79 | mockResponse.put("level", "Intermediate"); |
| 80 | mockResponse.put("reward", Map.of("experience", 1000, "points", 200)); |
| 81 | |
| 82 | // 模拟 taskService.getUserExperience 返回 |
| 83 | when(taskService.getUserExperience(userId)).thenReturn(mockResponse); |
| 84 | |
| 85 | // 创建请求体 |
| 86 | Map<String, Object> request = new HashMap<>(); |
| 87 | request.put("user_id", userId); |
| 88 | |
| 89 | // 调用控制器方法 |
| 90 | Map<String, Object> response = taskController.getExperience(request); |
| 91 | |
| 92 | // 验证返回的结果 |
| 93 | assertEquals(1500, response.get("current_experience")); |
| 94 | assertEquals("Intermediate", response.get("level")); |
| 95 | assertTrue(response.containsKey("reward")); |
| 96 | } |
| 97 | |
| 98 | // 测试获取当前的指引步骤接口 |
| 99 | @Test |
| 100 | void testGetNewStep() { |
| 101 | Long userId = 1L; |
| 102 | Map<String, Object> mockResponse = new HashMap<>(); |
| 103 | mockResponse.put("current_step", "step_1"); |
| 104 | mockResponse.put("total_steps", 5); |
| 105 | mockResponse.put("step_description", "Complete the introduction task"); |
| 106 | |
| 107 | // 模拟 taskService.getNewStep 返回 |
| 108 | when(taskService.getNewStep(userId)).thenReturn(mockResponse); |
| 109 | |
| 110 | // 调用控制器方法,改为通过请求参数传递 user_id |
| 111 | Map<String, Object> response = taskController.getNewStep(userId); |
| 112 | |
| 113 | // 验证返回的结果 |
| 114 | assertEquals("step_1", response.get("current_step")); |
| 115 | assertEquals(5, response.get("total_steps")); |
| 116 | assertEquals("Complete the introduction task", response.get("step_description")); |
| 117 | } |
| 118 | |
| 119 | // 测试更新进度接口 |
| 120 | @Test |
| 121 | void testUpdateProgress() { |
| 122 | Long userId = 1L; |
| 123 | String taskId = "task_123"; |
| 124 | Integer progress = 50; |
| 125 | Map<String, Object> mockResponse = new HashMap<>(); |
| 126 | mockResponse.put("status", "success"); |
| 127 | mockResponse.put("message", "进度已更新"); |
| 128 | |
| 129 | // 模拟 taskService.updateTaskProgress 返回 |
| 130 | when(taskService.updateTaskProgress(userId, taskId, progress)).thenReturn(mockResponse); |
| 131 | |
| 132 | // 创建请求体 |
| 133 | Map<String, Object> request = new HashMap<>(); |
| 134 | request.put("user_id", userId); |
| 135 | request.put("task_id", taskId); |
| 136 | request.put("progress", progress); |
| 137 | |
| 138 | // 调用控制器方法 |
| 139 | Map<String, Object> response = taskController.updateProgress(request); |
| 140 | |
| 141 | // 验证返回的结果 |
| 142 | assertEquals("success", response.get("status")); |
| 143 | assertEquals("进度已更新", response.get("message")); |
| 144 | } |
| 145 | |
| 146 | // 测试领取任务奖励接口 |
| 147 | @Test |
| 148 | void testRewardClaim() { |
| 149 | Long userId = 1L; |
| 150 | String taskId = "task_123"; |
| 151 | Map<String, Object> mockResponse = new HashMap<>(); |
| 152 | mockResponse.put("status", "success"); |
| 153 | mockResponse.put("message", "奖励已领取"); |
| 154 | mockResponse.put("reward", Map.of("experience", 1000, "points", 200)); |
| 155 | |
| 156 | // 模拟 taskService.claimReward 返回 |
| 157 | when(taskService.claimReward(userId, taskId)).thenReturn(mockResponse); |
| 158 | |
| 159 | // 创建请求体 |
| 160 | Map<String, Object> request = new HashMap<>(); |
| 161 | request.put("user_id", userId); |
| 162 | request.put("task_id", taskId); |
| 163 | |
| 164 | // 调用控制器方法 |
| 165 | Map<String, Object> response = taskController.rewardClaim(request); |
| 166 | |
| 167 | // 验证返回的结果 |
| 168 | assertEquals("success", response.get("status")); |
| 169 | assertEquals("奖励已领取", response.get("message")); |
| 170 | assertTrue(response.containsKey("reward")); |
| 171 | } |
| 172 | |
| 173 | // 测试检查任务奖励状态接口 |
| 174 | @Test |
| 175 | void testRewardReview() { |
| 176 | Long userId = 1L; |
| 177 | String taskId = "task_123"; |
| 178 | Map<String, Object> mockResponse = new HashMap<>(); |
| 179 | mockResponse.put("status", "reward_not_issued"); |
| 180 | mockResponse.put("message", "任务奖励未被领取"); |
| 181 | |
| 182 | // 模拟 taskService.checkRewardStatus 返回 |
| 183 | when(taskService.checkRewardStatus(userId, taskId)).thenReturn(mockResponse); |
| 184 | |
| 185 | // 创建请求体 |
| 186 | Map<String, Object> request = new HashMap<>(); |
| 187 | request.put("user_id", userId); |
| 188 | request.put("task_id", taskId); |
| 189 | |
| 190 | // 调用控制器方法 |
| 191 | Map<String, Object> response = taskController.rewardReview(request); |
| 192 | |
| 193 | // 验证返回的结果 |
| 194 | assertEquals("reward_not_issued", response.get("status")); |
| 195 | assertEquals("任务奖励未被领取", response.get("message")); |
| 196 | } |
| 197 | } |