| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.etotem.cfc.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.etotem.cfc.entity.GuideApplication;
- import com.etotem.cfc.mapper.GuideApplicationMapper;
- import com.etotem.cfc.mapper.GuideApplicationRecordMapper;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.boot.test.mock.mockito.MockBean;
- import javax.annotation.Resource;
- import java.util.*;
- import static org.junit.jupiter.api.Assertions.*;
- import static org.mockito.ArgumentMatchers.*;
- import static org.mockito.Mockito.*;
- @SpringBootTest
- public class GuideApplicationServiceTest {
- @Resource
- private GuideApplicationService guideApplicationService;
- @MockBean
- private GuideApplicationMapper guideApplicationMapper;
- @MockBean
- private GuideApplicationRecordMapper guideApplicationRecordMapper;
- private GuideApplication createApplication(Long id, Long userId, String type, String status) {
- GuideApplication app = new GuideApplication();
- app.setId(id);
- app.setUserId(userId);
- app.setType(type);
- app.setStatus(status);
- app.setCreatedAt(new Date());
- return app;
- }
- @Test
- public void applyNew_Success() {
- when(guideApplicationMapper.insert(any(GuideApplication.class))).thenReturn(1);
- boolean result = guideApplicationService.applyNew(1L, "junior", 100L, "proof.jpg");
- assertTrue(result);
- }
- @Test
- public void applyUpgrade_Success() {
- when(guideApplicationMapper.insert(any(GuideApplication.class))).thenReturn(1);
- boolean result = guideApplicationService.applyUpgrade(1L, "junior", "senior", "proof.jpg");
- assertTrue(result);
- }
- @Test
- public void getPendingApplications_WithData() {
- List<GuideApplication> apps = Arrays.asList(
- createApplication(1L, 1L, "new_apply", "pending"),
- createApplication(2L, 2L, "upgrade", "pending")
- );
- when(guideApplicationMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(apps);
- List<GuideApplication> result = guideApplicationService.getPendingApplications();
- assertNotNull(result);
- assertEquals(2, result.size());
- assertTrue(result.stream().allMatch(a -> "pending".equals(a.getStatus())));
- }
- @Test
- public void getPendingApplications_NoData() {
- when(guideApplicationMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(new ArrayList<>());
- List<GuideApplication> result = guideApplicationService.getPendingApplications();
- assertNotNull(result);
- assertTrue(result.isEmpty());
- }
- @Test
- public void approveApplication_Success() {
- GuideApplication app = createApplication(1L, 1L, "new_apply", "pending");
- when(guideApplicationMapper.selectById(1L)).thenReturn(app);
- when(guideApplicationMapper.updateById(any(GuideApplication.class))).thenReturn(1);
- boolean result = guideApplicationService.approveApplication(1L, 100L);
- assertTrue(result);
- assertEquals("approved", app.getStatus());
- assertEquals(100L, app.getReviewedBy());
- assertNotNull(app.getReviewedAt());
- }
- @Test
- public void approveApplication_NotFound_ReturnsFalse() {
- when(guideApplicationMapper.selectById(999L)).thenReturn(null);
- boolean result = guideApplicationService.approveApplication(999L, 100L);
- assertFalse(result);
- }
- @Test
- public void rejectApplication_Success() {
- GuideApplication app = createApplication(1L, 1L, "new_apply", "pending");
- when(guideApplicationMapper.selectById(1L)).thenReturn(app);
- when(guideApplicationMapper.updateById(any(GuideApplication.class))).thenReturn(1);
- boolean result = guideApplicationService.rejectApplication(1L, 100L, "资料不完整");
- assertTrue(result);
- assertEquals("rejected", app.getStatus());
- assertEquals("资料不完整", app.getRejectReason());
- assertEquals(100L, app.getReviewedBy());
- assertNotNull(app.getReviewedAt());
- }
- @Test
- public void rejectApplication_NotFound_ReturnsFalse() {
- when(guideApplicationMapper.selectById(999L)).thenReturn(null);
- boolean result = guideApplicationService.rejectApplication(999L, 100L, "原因");
- assertFalse(result);
- }
- }
|