| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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));
- }
- }
|