GuideApplicationServiceTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.etotem.cfc.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.etotem.cfc.entity.GuideApplication;
  4. import com.etotem.cfc.mapper.GuideApplicationMapper;
  5. import com.etotem.cfc.mapper.GuideApplicationRecordMapper;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.boot.test.mock.mockito.MockBean;
  9. import javax.annotation.Resource;
  10. import java.util.*;
  11. import static org.junit.jupiter.api.Assertions.*;
  12. import static org.mockito.ArgumentMatchers.*;
  13. import static org.mockito.Mockito.*;
  14. @SpringBootTest
  15. public class GuideApplicationServiceTest {
  16. @Resource
  17. private GuideApplicationService guideApplicationService;
  18. @MockBean
  19. private GuideApplicationMapper guideApplicationMapper;
  20. @MockBean
  21. private GuideApplicationRecordMapper guideApplicationRecordMapper;
  22. private GuideApplication createApplication(Long id, Long userId, String type, String status) {
  23. GuideApplication app = new GuideApplication();
  24. app.setId(id);
  25. app.setUserId(userId);
  26. app.setType(type);
  27. app.setStatus(status);
  28. app.setCreatedAt(new Date());
  29. return app;
  30. }
  31. @Test
  32. public void applyNew_Success() {
  33. when(guideApplicationMapper.insert(any(GuideApplication.class))).thenReturn(1);
  34. boolean result = guideApplicationService.applyNew(1L, "junior", 100L, "proof.jpg");
  35. assertTrue(result);
  36. }
  37. @Test
  38. public void applyUpgrade_Success() {
  39. when(guideApplicationMapper.insert(any(GuideApplication.class))).thenReturn(1);
  40. boolean result = guideApplicationService.applyUpgrade(1L, "junior", "senior", "proof.jpg");
  41. assertTrue(result);
  42. }
  43. @Test
  44. public void getPendingApplications_WithData() {
  45. List<GuideApplication> apps = Arrays.asList(
  46. createApplication(1L, 1L, "new_apply", "pending"),
  47. createApplication(2L, 2L, "upgrade", "pending")
  48. );
  49. when(guideApplicationMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(apps);
  50. List<GuideApplication> result = guideApplicationService.getPendingApplications();
  51. assertNotNull(result);
  52. assertEquals(2, result.size());
  53. assertTrue(result.stream().allMatch(a -> "pending".equals(a.getStatus())));
  54. }
  55. @Test
  56. public void getPendingApplications_NoData() {
  57. when(guideApplicationMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(new ArrayList<>());
  58. List<GuideApplication> result = guideApplicationService.getPendingApplications();
  59. assertNotNull(result);
  60. assertTrue(result.isEmpty());
  61. }
  62. @Test
  63. public void approveApplication_Success() {
  64. GuideApplication app = createApplication(1L, 1L, "new_apply", "pending");
  65. when(guideApplicationMapper.selectById(1L)).thenReturn(app);
  66. when(guideApplicationMapper.updateById(any(GuideApplication.class))).thenReturn(1);
  67. boolean result = guideApplicationService.approveApplication(1L, 100L);
  68. assertTrue(result);
  69. assertEquals("approved", app.getStatus());
  70. assertEquals(100L, app.getReviewedBy());
  71. assertNotNull(app.getReviewedAt());
  72. }
  73. @Test
  74. public void approveApplication_NotFound_ReturnsFalse() {
  75. when(guideApplicationMapper.selectById(999L)).thenReturn(null);
  76. boolean result = guideApplicationService.approveApplication(999L, 100L);
  77. assertFalse(result);
  78. }
  79. @Test
  80. public void rejectApplication_Success() {
  81. GuideApplication app = createApplication(1L, 1L, "new_apply", "pending");
  82. when(guideApplicationMapper.selectById(1L)).thenReturn(app);
  83. when(guideApplicationMapper.updateById(any(GuideApplication.class))).thenReturn(1);
  84. boolean result = guideApplicationService.rejectApplication(1L, 100L, "资料不完整");
  85. assertTrue(result);
  86. assertEquals("rejected", app.getStatus());
  87. assertEquals("资料不完整", app.getRejectReason());
  88. assertEquals(100L, app.getReviewedBy());
  89. assertNotNull(app.getReviewedAt());
  90. }
  91. @Test
  92. public void rejectApplication_NotFound_ReturnsFalse() {
  93. when(guideApplicationMapper.selectById(999L)).thenReturn(null);
  94. boolean result = guideApplicationService.rejectApplication(999L, 100L, "原因");
  95. assertFalse(result);
  96. }
  97. }