提交 | 用户 | 时间
|
e7c126
|
1 |
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
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 ${jakartaPackage}.annotation.Resource; |
|
8 |
|
|
9 |
import ${baseFrameworkPackage}.test.core.ut.BaseDbUnitTest; |
|
10 |
|
|
11 |
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
|
12 |
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
|
13 |
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
|
14 |
import ${PageResultClassName}; |
|
15 |
|
|
16 |
import ${jakartaPackage}.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 ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; |
|
23 |
import static ${baseFrameworkPackage}.test.core.util.AssertUtils.*; |
|
24 |
import static ${baseFrameworkPackage}.test.core.util.RandomUtils.*; |
|
25 |
import static ${LocalDateTimeUtilsClassName}.*; |
|
26 |
import static ${ObjectUtilsClassName}.*; |
|
27 |
import static ${DateUtilsClassName}.*; |
|
28 |
import static org.junit.jupiter.api.Assertions.*; |
|
29 |
import static org.mockito.Mockito.*; |
|
30 |
|
|
31 |
## 字段模板 |
|
32 |
#macro(getPageCondition $VO) |
|
33 |
// mock 数据 |
|
34 |
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> { // 等会查询到 |
|
35 |
#foreach ($column in $columns) |
|
36 |
#if (${column.listOperation}) |
|
37 |
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
|
38 |
o.set$JavaField(null); |
|
39 |
#end |
|
40 |
#end |
|
41 |
}); |
|
42 |
${classNameVar}Mapper.insert(db${simpleClassName}); |
|
43 |
#foreach ($column in $columns) |
|
44 |
#if (${column.listOperation}) |
|
45 |
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
|
46 |
// 测试 ${column.javaField} 不匹配 |
|
47 |
${classNameVar}Mapper.insert(cloneIgnoreId(db${simpleClassName}, o -> o.set$JavaField(null))); |
|
48 |
#end |
|
49 |
#end |
|
50 |
// 准备参数 |
|
51 |
${sceneEnum.prefixClass}${table.className}${VO} reqVO = new ${sceneEnum.prefixClass}${table.className}${VO}(); |
|
52 |
#foreach ($column in $columns) |
|
53 |
#if (${column.listOperation}) |
|
54 |
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
|
55 |
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况 |
|
56 |
reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); |
|
57 |
#else |
|
58 |
reqVO.set$JavaField(null); |
|
59 |
#end |
|
60 |
#end |
|
61 |
#end |
|
62 |
#end |
|
63 |
/** |
|
64 |
* {@link ${table.className}ServiceImpl} 的单元测试类 |
|
65 |
* |
|
66 |
* @author ${table.author} |
|
67 |
*/ |
|
68 |
@Import(${table.className}ServiceImpl.class) |
|
69 |
public class ${table.className}ServiceImplTest extends BaseDbUnitTest { |
|
70 |
|
|
71 |
@Resource |
|
72 |
private ${table.className}ServiceImpl ${classNameVar}Service; |
|
73 |
|
|
74 |
@Resource |
|
75 |
private ${table.className}Mapper ${classNameVar}Mapper; |
|
76 |
|
|
77 |
@Test |
|
78 |
public void testCreate${simpleClassName}_success() { |
|
79 |
// 准备参数 |
|
80 |
${sceneEnum.prefixClass}${table.className}SaveReqVO createReqVO = randomPojo(${sceneEnum.prefixClass}${table.className}SaveReqVO.class).setId(null); |
|
81 |
|
|
82 |
// 调用 |
|
83 |
${primaryColumn.javaType} ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(createReqVO); |
|
84 |
// 断言 |
|
85 |
assertNotNull(${classNameVar}Id); |
|
86 |
// 校验记录的属性是否正确 |
|
87 |
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id); |
|
88 |
assertPojoEquals(createReqVO, ${classNameVar}, "id"); |
|
89 |
} |
|
90 |
|
|
91 |
@Test |
|
92 |
public void testUpdate${simpleClassName}_success() { |
|
93 |
// mock 数据 |
|
94 |
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); |
|
95 |
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 |
|
96 |
// 准备参数 |
|
97 |
${sceneEnum.prefixClass}${table.className}SaveReqVO updateReqVO = randomPojo(${sceneEnum.prefixClass}${table.className}SaveReqVO.class, o -> { |
|
98 |
o.setId(db${simpleClassName}.getId()); // 设置更新的 ID |
|
99 |
}); |
|
100 |
|
|
101 |
// 调用 |
|
102 |
${classNameVar}Service.update${simpleClassName}(updateReqVO); |
|
103 |
// 校验是否更新正确 |
|
104 |
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(updateReqVO.getId()); // 获取最新的 |
|
105 |
assertPojoEquals(updateReqVO, ${classNameVar}); |
|
106 |
} |
|
107 |
|
|
108 |
@Test |
|
109 |
public void testUpdate${simpleClassName}_notExists() { |
|
110 |
// 准备参数 |
|
111 |
${sceneEnum.prefixClass}${table.className}SaveReqVO updateReqVO = randomPojo(${sceneEnum.prefixClass}${table.className}SaveReqVO.class); |
|
112 |
|
|
113 |
// 调用, 并断言异常 |
|
114 |
assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(updateReqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
|
115 |
} |
|
116 |
|
|
117 |
@Test |
|
118 |
public void testDelete${simpleClassName}_success() { |
|
119 |
// mock 数据 |
|
120 |
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); |
|
121 |
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 |
|
122 |
// 准备参数 |
|
123 |
${primaryColumn.javaType} id = db${simpleClassName}.getId(); |
|
124 |
|
|
125 |
// 调用 |
|
126 |
${classNameVar}Service.delete${simpleClassName}(id); |
|
127 |
// 校验数据不存在了 |
|
128 |
assertNull(${classNameVar}Mapper.selectById(id)); |
|
129 |
} |
|
130 |
|
|
131 |
@Test |
|
132 |
public void testDelete${simpleClassName}_notExists() { |
|
133 |
// 准备参数 |
|
134 |
${primaryColumn.javaType} id = random${primaryColumn.javaType}Id(); |
|
135 |
|
|
136 |
// 调用, 并断言异常 |
|
137 |
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
|
138 |
} |
|
139 |
|
|
140 |
## 特殊:树表专属逻辑(树不需要分页接口) |
|
141 |
#if ( $table.templateType != 2 ) |
|
142 |
@Test |
|
143 |
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
|
144 |
public void testGet${simpleClassName}Page() { |
|
145 |
#getPageCondition("PageReqVO") |
|
146 |
|
|
147 |
// 调用 |
|
148 |
PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(reqVO); |
|
149 |
// 断言 |
|
150 |
assertEquals(1, pageResult.getTotal()); |
|
151 |
assertEquals(1, pageResult.getList().size()); |
|
152 |
assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0)); |
|
153 |
} |
|
154 |
#else |
|
155 |
@Test |
|
156 |
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
|
157 |
public void testGet${simpleClassName}List() { |
|
158 |
#getPageCondition("ListReqVO") |
|
159 |
|
|
160 |
// 调用 |
|
161 |
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(reqVO); |
|
162 |
// 断言 |
|
163 |
assertEquals(1, list.size()); |
|
164 |
assertPojoEquals(db${simpleClassName}, list.get(0)); |
|
165 |
} |
|
166 |
#end |
|
167 |
|
|
168 |
} |