Commit 816808f8fad426e75555f75edea61a5af7f1c4b8
1 parent
aaf762e7
进一步完善 ut 的 vm 模板
Showing
2 changed files
with
59 additions
and
106 deletions
src/main/resources/codegen/java/test/serviceTest.vm
| @@ -12,17 +12,53 @@ import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}. | @@ -12,17 +12,53 @@ import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}. | ||
| 12 | import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; | 12 | import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 13 | import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | 13 | import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
| 14 | import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; | 14 | import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
| 15 | +import ${basePackage}.util.object.ObjectUtils; | ||
| 16 | +import ${PageResultClassName}; | ||
| 15 | 17 | ||
| 16 | import javax.annotation.Resource; | 18 | import javax.annotation.Resource; |
| 19 | +import java.util.*; | ||
| 17 | 20 | ||
| 18 | import static cn.hutool.core.util.RandomUtil.*; | 21 | import static cn.hutool.core.util.RandomUtil.*; |
| 19 | import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; | 22 | import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; |
| 20 | -import static cn.iocoder.dashboard.util.AssertUtils.*; | ||
| 21 | -import static cn.iocoder.dashboard.util.RandomUtils.*; | ||
| 22 | -import static cn.iocoder.dashboard.util.date.DateUtils.*; | 23 | +import static ${basePackage}.util.AssertUtils.*; |
| 24 | +import static ${basePackage}.util.RandomUtils.*; | ||
| 25 | +import static ${basePackage}.util.date.DateUtils.*; | ||
| 23 | import static org.junit.jupiter.api.Assertions.*; | 26 | import static org.junit.jupiter.api.Assertions.*; |
| 24 | import static org.mockito.Mockito.*; | 27 | import static org.mockito.Mockito.*; |
| 25 | 28 | ||
| 29 | +## 字段模板 | ||
| 30 | +#macro(getPageCondition $VO) | ||
| 31 | + // mock 数据 | ||
| 32 | + ${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到 | ||
| 33 | + #foreach ($column in $columns) | ||
| 34 | + #if (${column.listOperation}) | ||
| 35 | + #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 | ||
| 36 | + o.set$JavaField(null); | ||
| 37 | + #end | ||
| 38 | + #end | ||
| 39 | + }); | ||
| 40 | + ${classNameVar}Mapper.insert(db${simpleClassName}); | ||
| 41 | + #foreach ($column in $columns) | ||
| 42 | + #if (${column.listOperation}) | ||
| 43 | + #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 | ||
| 44 | + // 测试 ${column.javaField} 不匹配 | ||
| 45 | + ${classNameVar}Mapper.insert(ObjectUtils.clone(db${simpleClassName}, o -> o.set$JavaField(null))); | ||
| 46 | + #end | ||
| 47 | + #end | ||
| 48 | + // 准备参数 | ||
| 49 | + ${table.className}${VO} reqVO = new ${table.className}${VO}(); | ||
| 50 | + #foreach ($column in $columns) | ||
| 51 | + #if (${column.listOperation}) | ||
| 52 | + #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 | ||
| 53 | + #if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况 | ||
| 54 | + reqVO.setBegin${JavaField}(null); | ||
| 55 | + reqVO.setEnd${JavaField}(null); | ||
| 56 | + #else | ||
| 57 | + reqVO.set$JavaField(null); | ||
| 58 | + #end | ||
| 59 | + #end | ||
| 60 | + #end | ||
| 61 | +#end | ||
| 26 | /** | 62 | /** |
| 27 | * {@link ${table.className}ServiceImpl} 的单元测试类 | 63 | * {@link ${table.className}ServiceImpl} 的单元测试类 |
| 28 | * | 64 | * |
| @@ -97,15 +133,29 @@ public class ${table.className}ServiceTest extends BaseSpringBootUnitTest { | @@ -97,15 +133,29 @@ public class ${table.className}ServiceTest extends BaseSpringBootUnitTest { | ||
| 97 | 133 | ||
| 98 | // 调用, 并断言异常 | 134 | // 调用, 并断言异常 |
| 99 | assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); | 135 | assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
| 100 | - } | 136 | + } |
| 101 | 137 | ||
| 102 | - @Test | 138 | + @Test // TODO 请修改 null 为需要的值 |
| 103 | public void testGet${simpleClassName}Page() { | 139 | public void testGet${simpleClassName}Page() { |
| 104 | - // mock 数据 | ||
| 105 | - ${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到 | 140 | + #getPageCondition("PageReqVO") |
| 141 | + | ||
| 142 | + // 调用 | ||
| 143 | + PageResult<${table.className}DO> pageResult = configService.getConfigPage(reqVO); | ||
| 144 | + // 断言 | ||
| 145 | + assertEquals(1, pageResult.getTotal()); | ||
| 146 | + assertEquals(1, pageResult.getList().size()); | ||
| 147 | + assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0)); | ||
| 148 | + } | ||
| 106 | 149 | ||
| 107 | - }); | ||
| 108 | - ${classNameVar}Mapper.insert(db${simpleClassName}); | 150 | + @Test // TODO 请修改 null 为需要的值 |
| 151 | + public void testGet${simpleClassName}List() { | ||
| 152 | + #getPageCondition("ExportReqVO") | ||
| 153 | + | ||
| 154 | + // 调用 | ||
| 155 | + PageResult<${table.className}DO> list = configService.getConfigPage(reqVO); | ||
| 156 | + // 断言 | ||
| 157 | + assertEquals(1, list.size()); | ||
| 158 | + assertPojoEquals(db${simpleClassName}, list.get(0)); | ||
| 109 | } | 159 | } |
| 110 | 160 | ||
| 111 | } | 161 | } |
src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfConfigServiceTest.java deleted
| 1 | -package cn.iocoder.dashboard.modules.infra.service.job; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.dashboard.BaseSpringBootUnitTest; | ||
| 4 | -import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO; | ||
| 5 | -import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigUpdateReqVO; | ||
| 6 | -import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO; | ||
| 7 | -import cn.iocoder.dashboard.modules.infra.dal.mysql.config.InfConfigMapper; | ||
| 8 | -import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl; | ||
| 9 | -import org.junit.jupiter.api.Test; | ||
| 10 | - | ||
| 11 | -import javax.annotation.Resource; | ||
| 12 | - | ||
| 13 | -import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS; | ||
| 14 | -import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||
| 15 | -import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | ||
| 16 | -import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; | ||
| 17 | -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||
| 18 | -import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| 19 | -import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 20 | - | ||
| 21 | -/** | ||
| 22 | -* {@link InfConfigServiceImpl} 的单元测试类 | ||
| 23 | -* | ||
| 24 | -* @author 芋艿 | ||
| 25 | -*/ | ||
| 26 | -public class InfConfigServiceTest extends BaseSpringBootUnitTest { | ||
| 27 | - | ||
| 28 | - @Resource | ||
| 29 | - private InfConfigServiceImpl configService; | ||
| 30 | - | ||
| 31 | - @Resource | ||
| 32 | - private InfConfigMapper configMapper; | ||
| 33 | - | ||
| 34 | - @Test | ||
| 35 | - public void testCreateConfig_success() { | ||
| 36 | - // 准备参数 | ||
| 37 | - InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class); | ||
| 38 | - | ||
| 39 | - // 调用 | ||
| 40 | - Long configId = configService.createConfig(reqVO); | ||
| 41 | - // 断言 | ||
| 42 | - assertNotNull(configId); | ||
| 43 | - // 校验记录的属性是否正确 | ||
| 44 | - InfConfigDO config = configMapper.selectById(configId); | ||
| 45 | - assertPojoEquals(reqVO, config); | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - @Test | ||
| 49 | - public void testUpdateConfig_success() { | ||
| 50 | - // mock 数据 | ||
| 51 | - InfConfigDO dbConfig = randomPojo(InfConfigDO.class); | ||
| 52 | - configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 | ||
| 53 | - // 准备参数 | ||
| 54 | - InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> { | ||
| 55 | - o.setId(dbConfig.getId()); // 设置更新的 ID | ||
| 56 | - }); | ||
| 57 | - | ||
| 58 | - // 调用 | ||
| 59 | - configService.updateConfig(reqVO); | ||
| 60 | - // 校验是否更新正确 | ||
| 61 | - InfConfigDO config = configMapper.selectById(reqVO.getId()); // 获取最新的 | ||
| 62 | - assertPojoEquals(reqVO, config); | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - @Test | ||
| 66 | - public void testUpdateConfig_notExists() { | ||
| 67 | - // 准备参数 | ||
| 68 | - InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class); | ||
| 69 | - | ||
| 70 | - // 调用, 并断言异常 | ||
| 71 | - assertServiceException(() -> configService.updateConfig(reqVO), CONFIG_NOT_EXISTS); | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - @Test | ||
| 75 | - public void testDeleteConfig_success() { | ||
| 76 | - // mock 数据 | ||
| 77 | - InfConfigDO dbConfig = randomPojo(InfConfigDO.class); | ||
| 78 | - configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 | ||
| 79 | - // 准备参数 | ||
| 80 | - Long id = dbConfig.getId(); | ||
| 81 | - | ||
| 82 | - // 调用 | ||
| 83 | - configService.deleteConfig(id); | ||
| 84 | - // 校验数据不存在了 | ||
| 85 | - assertNull(configMapper.selectById(id)); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - @Test | ||
| 89 | - public void testDeleteConfig_notExists() { | ||
| 90 | - // 准备参数 | ||
| 91 | - Long id = randomLongId(); | ||
| 92 | - | ||
| 93 | - // 调用, 并断言异常 | ||
| 94 | - assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | -} |