TaskServiceTest.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.zhixingyijia.service;
  2. import com.zhixingyijia.entity.PointsLog;
  3. import com.zhixingyijia.entity.Task;
  4. import com.zhixingyijia.mapper.PointsLogMapper;
  5. import com.zhixingyijia.mapper.TaskMapper;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.jupiter.api.extension.ExtendWith;
  8. import org.mockito.InjectMocks;
  9. import org.mockito.Mock;
  10. import org.mockito.junit.jupiter.MockitoExtension;
  11. import org.springframework.test.util.ReflectionTestUtils;
  12. import java.time.LocalDateTime;
  13. import static org.junit.jupiter.api.Assertions.*;
  14. import static org.mockito.ArgumentMatchers.any;
  15. import static org.mockito.Mockito.*;
  16. /**
  17. * TaskService�元测试
  18. */
  19. @ExtendWith(MockitoExtension.class)
  20. class TaskServiceTest {
  21. @InjectMocks
  22. private TaskService taskService;
  23. @Mock
  24. private TaskMapper taskMapper;
  25. @Mock
  26. private PointsLogMapper pointsLogMapper;
  27. @Test
  28. void testFindById() {
  29. // 准备测试数�
  30. Long taskId = 1L;
  31. Task expectedTask = new Task();
  32. expectedTask.setId(taskId);
  33. expectedTask.setTitle("测试任务");
  34. // 设置mock行为
  35. when(taskMapper.findById(taskId)).thenReturn(expectedTask);
  36. // 执行测试
  37. Task result = taskService.findById(taskId);
  38. // 验�结果
  39. assertNotNull(result);
  40. assertEquals(expectedTask.getId(), result.getId());
  41. assertEquals(expectedTask.getTitle(), result.getTitle());
  42. // 验�mock调用
  43. verify(taskMapper, times(1)).findById(taskId);
  44. }
  45. @Test
  46. void testCreateTask() {
  47. // 准备测试数�
  48. Task task = new Task();
  49. task.setTitle("测试任务");
  50. task.setPoints(2);
  51. // 设置mock行为
  52. doAnswer(invocation -> {
  53. Task insertedTask = invocation.getArgument(0);
  54. insertedTask.setId(1L);
  55. return null;
  56. }).when(taskMapper).insert(any(Task.class));
  57. // 执行测试
  58. Task result = taskService.createTask(task);
  59. // 验�结果
  60. assertNotNull(result);
  61. assertEquals(1L, result.getId());
  62. assertEquals("pending", result.getStatus());
  63. // 验�mock调用
  64. verify(taskMapper, times(1)).insert(task);
  65. }
  66. @Test
  67. void testCompleteTaskRequiresReview() {
  68. // 准备测试数�
  69. Long taskId = 1L;
  70. Task task = new Task();
  71. task.setId(taskId);
  72. task.setTitle("测试任务");
  73. task.setPoints(2);
  74. task.setStatus("pending");
  75. task.setNeedReview(true);
  76. task.setDeadline(LocalDateTime.now().plusHours(2));
  77. // 设置mock行为
  78. when(taskMapper.findById(taskId)).thenReturn(task);
  79. // 执行测试
  80. Task result = taskService.completeTask(taskId, "photo.jpg", "voice.mp3");
  81. // 验�结果
  82. assertNotNull(result);
  83. assertEquals("completed", result.getStatus());
  84. assertEquals("pending", result.getReviewStatus());
  85. assertEquals("photo.jpg", result.getReviewPhotoUrl());
  86. assertEquals("voice.mp3", result.getReviewVoiceUrl());
  87. assertNotNull(result.getCompletedAt());
  88. // 验�mock调用
  89. verify(taskMapper, times(1)).findById(taskId);
  90. verify(taskMapper, times(1)).update(task);
  91. verify(pointsLogMapper, never()).insert(any(PointsLog.class));
  92. }
  93. @Test
  94. void testCompleteTaskNoReview() {
  95. // 准备测试数�
  96. Long taskId = 1L;
  97. Task task = new Task();
  98. task.setId(taskId);
  99. task.setTitle("测试任务");
  100. task.setPoints(2);
  101. task.setStatus("pending");
  102. task.setNeedReview(false);
  103. task.setDeadline(LocalDateTime.now().plusHours(2));
  104. // 设置mock行为
  105. when(taskMapper.findById(taskId)).thenReturn(task);
  106. // 执行测试
  107. Task result = taskService.completeTask(taskId, null, null);
  108. // 验�结果
  109. assertNotNull(result);
  110. assertEquals("approved", result.getStatus());
  111. assertNotNull(result.getCompletedAt());
  112. // 验�mock调用
  113. verify(taskMapper, times(1)).findById(taskId);
  114. verify(taskMapper, times(1)).update(task);
  115. verify(pointsLogMapper, times(1)).insert(any(PointsLog.class));
  116. }
  117. @Test
  118. void testCompleteTaskAlreadyCompleted() {
  119. // 准备测试数�
  120. Long taskId = 1L;
  121. Task task = new Task();
  122. task.setId(taskId);
  123. task.setTitle("测试任务");
  124. task.setStatus("completed");
  125. // 设置mock行为
  126. when(taskMapper.findById(taskId)).thenReturn(task);
  127. // 执行测试并验�异� assertThrows(RuntimeException.class, () -> {
  128. taskService.completeTask(taskId, null, null);
  129. });
  130. // 验�mock调用
  131. verify(taskMapper, times(1)).findById(taskId);
  132. verify(taskMapper, never()).update(any(Task.class));
  133. verify(pointsLogMapper, never()).insert(any(PointsLog.class));
  134. }
  135. @Test
  136. void testReviewTaskApproved() {
  137. // 准备测试数�
  138. Long taskId = 1L;
  139. Long reviewerId = 1L;
  140. Task task = new Task();
  141. task.setId(taskId);
  142. task.setTitle("测试任务");
  143. task.setPoints(2);
  144. task.setStatus("completed");
  145. task.setCompletedEarly(true);
  146. // 设置mock行为
  147. when(taskMapper.findById(taskId)).thenReturn(task);
  148. // 执行测试
  149. Task result = taskService.reviewTask(taskId, "approved", "审核通过", reviewerId);
  150. // 验�结果
  151. assertNotNull(result);
  152. assertEquals("approved", result.getStatus());
  153. assertEquals("approved", result.getReviewStatus());
  154. assertEquals("审核通过", result.getReviewComment());
  155. assertEquals(reviewerId, result.getReviewedBy());
  156. assertNotNull(result.getReviewedAt());
  157. // 验�mock调用
  158. verify(taskMapper, times(1)).findById(taskId);
  159. verify(taskMapper, times(1)).update(task);
  160. verify(pointsLogMapper, times(1)).insert(any(PointsLog.class));
  161. }
  162. @Test
  163. void testReviewTaskRejected() {
  164. // 准备测试数�
  165. Long taskId = 1L;
  166. Long reviewerId = 1L;
  167. Task task = new Task();
  168. task.setId(taskId);
  169. task.setTitle("测试任务");
  170. task.setPoints(2);
  171. task.setStatus("completed");
  172. // 设置mock行为
  173. when(taskMapper.findById(taskId)).thenReturn(task);
  174. // 执行测试
  175. Task result = taskService.reviewTask(taskId, "rejected", "审核拒�", reviewerId);
  176. // 验�结果
  177. assertNotNull(result);
  178. assertEquals("rejected", result.getStatus());
  179. assertEquals("rejected", result.getReviewStatus());
  180. assertEquals("审核拒�", result.getReviewComment());
  181. assertEquals(reviewerId, result.getReviewedBy());
  182. assertNotNull(result.getReviewedAt());
  183. // 验�mock调用
  184. verify(taskMapper, times(1)).findById(taskId);
  185. verify(taskMapper, times(1)).update(task);
  186. verify(pointsLogMapper, never()).insert(any(PointsLog.class));
  187. }
  188. }