潘志宝
2024-09-09 ed81b7371e376df35448b81531d30dd9024bd44a
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.demo;
H 2
3 import org.junit.jupiter.api.Disabled;
4 import org.junit.jupiter.api.Test;
5 import org.springframework.boot.test.mock.mockito.MockBean;
6
7 import javax.annotation.Resource;
8
9 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
10
11 import com.iailab.module.infra.controller.admin.demo.vo.*;
12 import com.iailab.module.infra.dal.dataobject.demo.InfraStudentDO;
13 import com.iailab.module.infra.dal.mysql.demo.InfraStudentMapper;
14 import com.iailab.framework.common.pojo.PageResult;
15
16 import javax.annotation.Resource;
17 import org.springframework.context.annotation.Import;
18 import java.util.*;
19 import java.time.LocalDateTime;
20
21 import static cn.hutool.core.util.RandomUtil.*;
22 import static com.iailab.module.infra.enums.ErrorCodeConstants.*;
23 import static com.iailab.framework.test.core.util.AssertUtils.*;
24 import static com.iailab.framework.test.core.util.RandomUtils.*;
25 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.*;
26 import static com.iailab.framework.common.util.object.ObjectUtils.*;
27 import static com.iailab.framework.common.util.date.DateUtils.*;
28 import static org.junit.jupiter.api.Assertions.*;
29 import static org.mockito.Mockito.*;
30
31 /**
32  * {@link InfraStudentServiceImpl} 的单元测试类
33  *
34  * @author iailab
35  */
36 @Import(InfraStudentServiceImpl.class)
37 public class InfraStudentServiceImplTest extends BaseDbUnitTest {
38
39     @Resource
40     private InfraStudentServiceImpl studentService;
41
42     @Resource
43     private InfraStudentMapper studentMapper;
44
45     @Test
46     public void testCreateStudent_success() {
47         // 准备参数
48         InfraStudentSaveReqVO createReqVO = randomPojo(InfraStudentSaveReqVO.class).setId(null);
49
50         // 调用
51         Long studentId = studentService.createStudent(createReqVO);
52         // 断言
53         assertNotNull(studentId);
54         // 校验记录的属性是否正确
55         InfraStudentDO student = studentMapper.selectById(studentId);
56         assertPojoEquals(createReqVO, student, "id");
57     }
58
59     @Test
60     public void testUpdateStudent_success() {
61         // mock 数据
62         InfraStudentDO dbStudent = randomPojo(InfraStudentDO.class);
63         studentMapper.insert(dbStudent);// @Sql: 先插入出一条存在的数据
64         // 准备参数
65         InfraStudentSaveReqVO updateReqVO = randomPojo(InfraStudentSaveReqVO.class, o -> {
66             o.setId(dbStudent.getId()); // 设置更新的 ID
67         });
68
69         // 调用
70         studentService.updateStudent(updateReqVO);
71         // 校验是否更新正确
72         InfraStudentDO student = studentMapper.selectById(updateReqVO.getId()); // 获取最新的
73         assertPojoEquals(updateReqVO, student);
74     }
75
76     @Test
77     public void testUpdateStudent_notExists() {
78         // 准备参数
79         InfraStudentSaveReqVO updateReqVO = randomPojo(InfraStudentSaveReqVO.class);
80
81         // 调用, 并断言异常
82         assertServiceException(() -> studentService.updateStudent(updateReqVO), STUDENT_NOT_EXISTS);
83     }
84
85     @Test
86     public void testDeleteStudent_success() {
87         // mock 数据
88         InfraStudentDO dbStudent = randomPojo(InfraStudentDO.class);
89         studentMapper.insert(dbStudent);// @Sql: 先插入出一条存在的数据
90         // 准备参数
91         Long id = dbStudent.getId();
92
93         // 调用
94         studentService.deleteStudent(id);
95        // 校验数据不存在了
96        assertNull(studentMapper.selectById(id));
97     }
98
99     @Test
100     public void testDeleteStudent_notExists() {
101         // 准备参数
102         Long id = randomLongId();
103
104         // 调用, 并断言异常
105         assertServiceException(() -> studentService.deleteStudent(id), STUDENT_NOT_EXISTS);
106     }
107
108     @Test
109     @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
110     public void testGetStudentPage() {
111        // mock 数据
112        InfraStudentDO dbStudent = randomPojo(InfraStudentDO.class, o -> { // 等会查询到
113            o.setName(null);
114            o.setBirthday(null);
115            o.setSex(null);
116            o.setEnabled(null);
117            o.setCreateTime(null);
118        });
119        studentMapper.insert(dbStudent);
120        // 测试 name 不匹配
121        studentMapper.insert(cloneIgnoreId(dbStudent, o -> o.setName(null)));
122        // 测试 birthday 不匹配
123        studentMapper.insert(cloneIgnoreId(dbStudent, o -> o.setBirthday(null)));
124        // 测试 sex 不匹配
125        studentMapper.insert(cloneIgnoreId(dbStudent, o -> o.setSex(null)));
126        // 测试 enabled 不匹配
127        studentMapper.insert(cloneIgnoreId(dbStudent, o -> o.setEnabled(null)));
128        // 测试 createTime 不匹配
129        studentMapper.insert(cloneIgnoreId(dbStudent, o -> o.setCreateTime(null)));
130        // 准备参数
131        InfraStudentPageReqVO reqVO = new InfraStudentPageReqVO();
132        reqVO.setName(null);
133        reqVO.setBirthday(null);
134        reqVO.setSex(null);
135        reqVO.setEnabled(null);
136        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
137
138        // 调用
139        PageResult<InfraStudentDO> pageResult = studentService.getStudentPage(reqVO);
140        // 断言
141        assertEquals(1, pageResult.getTotal());
142        assertEquals(1, pageResult.getList().size());
143        assertPojoEquals(dbStudent, pageResult.getList().get(0));
144     }
145
146 }