package com.zhixingyijia.service; import com.zhixingyijia.entity.PointsLog; import com.zhixingyijia.entity.Task; import com.zhixingyijia.mapper.PointsLogMapper; import com.zhixingyijia.mapper.TaskMapper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import java.time.LocalDateTime; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; /** * TaskService单元测试 */ @ExtendWith(MockitoExtension.class) class TaskServiceTest { @InjectMocks private TaskService taskService; @Mock private TaskMapper taskMapper; @Mock private PointsLogMapper pointsLogMapper; @Test void testFindById() { // 准备测试数据 Long taskId = 1L; Task expectedTask = new Task(); expectedTask.setId(taskId); expectedTask.setTitle("测试任务"); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(expectedTask); // 执行测试 Task result = taskService.findById(taskId); // 验证结果 assertNotNull(result); assertEquals(expectedTask.getId(), result.getId()); assertEquals(expectedTask.getTitle(), result.getTitle()); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); } @Test void testCreateTask() { // 准备测试数据 Task task = new Task(); task.setTitle("测试任务"); task.setPoints(2); // 设置mock行为 doAnswer(invocation -> { Task insertedTask = invocation.getArgument(0); insertedTask.setId(1L); return null; }).when(taskMapper).insert(any(Task.class)); // 执行测试 Task result = taskService.createTask(task); // 验证结果 assertNotNull(result); assertEquals(1L, result.getId()); assertEquals("pending", result.getStatus()); // 验证mock调用 verify(taskMapper, times(1)).insert(task); } @Test void testCompleteTaskRequiresReview() { // 准备测试数据 Long taskId = 1L; Task task = new Task(); task.setId(taskId); task.setTitle("测试任务"); task.setPoints(2); task.setStatus("pending"); task.setNeedReview(true); task.setDeadline(LocalDateTime.now().plusHours(2)); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(task); // 执行测试 Task result = taskService.completeTask(taskId, "photo.jpg", "voice.mp3"); // 验证结果 assertNotNull(result); assertEquals("completed", result.getStatus()); assertEquals("pending", result.getReviewStatus()); assertEquals("photo.jpg", result.getReviewPhotoUrl()); assertEquals("voice.mp3", result.getReviewVoiceUrl()); assertNotNull(result.getCompletedAt()); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); verify(taskMapper, times(1)).update(task); verify(pointsLogMapper, never()).insert(any(PointsLog.class)); } @Test void testCompleteTaskNoReview() { // 准备测试数据 Long taskId = 1L; Task task = new Task(); task.setId(taskId); task.setTitle("测试任务"); task.setPoints(2); task.setStatus("pending"); task.setNeedReview(false); task.setDeadline(LocalDateTime.now().plusHours(2)); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(task); // 执行测试 Task result = taskService.completeTask(taskId, null, null); // 验证结果 assertNotNull(result); assertEquals("approved", result.getStatus()); assertNotNull(result.getCompletedAt()); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); verify(taskMapper, times(1)).update(task); verify(pointsLogMapper, times(1)).insert(any(PointsLog.class)); } @Test void testCompleteTaskAlreadyCompleted() { // 准备测试数据 Long taskId = 1L; Task task = new Task(); task.setId(taskId); task.setTitle("测试任务"); task.setStatus("completed"); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(task); // 执行测试并验证异å¸? assertThrows(RuntimeException.class, () -> { taskService.completeTask(taskId, null, null); }); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); verify(taskMapper, never()).update(any(Task.class)); verify(pointsLogMapper, never()).insert(any(PointsLog.class)); } @Test void testReviewTaskApproved() { // 准备测试数据 Long taskId = 1L; Long reviewerId = 1L; Task task = new Task(); task.setId(taskId); task.setTitle("测试任务"); task.setPoints(2); task.setStatus("completed"); task.setCompletedEarly(true); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(task); // 执行测试 Task result = taskService.reviewTask(taskId, "approved", "审核通过", reviewerId); // 验证结果 assertNotNull(result); assertEquals("approved", result.getStatus()); assertEquals("approved", result.getReviewStatus()); assertEquals("审核通过", result.getReviewComment()); assertEquals(reviewerId, result.getReviewedBy()); assertNotNull(result.getReviewedAt()); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); verify(taskMapper, times(1)).update(task); verify(pointsLogMapper, times(1)).insert(any(PointsLog.class)); } @Test void testReviewTaskRejected() { // 准备测试数据 Long taskId = 1L; Long reviewerId = 1L; Task task = new Task(); task.setId(taskId); task.setTitle("测试任务"); task.setPoints(2); task.setStatus("completed"); // 设置mock行为 when(taskMapper.findById(taskId)).thenReturn(task); // 执行测试 Task result = taskService.reviewTask(taskId, "rejected", "审核拒绝", reviewerId); // 验证结果 assertNotNull(result); assertEquals("rejected", result.getStatus()); assertEquals("rejected", result.getReviewStatus()); assertEquals("审核拒绝", result.getReviewComment()); assertEquals(reviewerId, result.getReviewedBy()); assertNotNull(result.getReviewedAt()); // 验证mock调用 verify(taskMapper, times(1)).findById(taskId); verify(taskMapper, times(1)).update(task); verify(pointsLogMapper, never()).insert(any(PointsLog.class)); } }