进一步完善 ut 的 vm 模板

This commit is contained in:
YunaiV 2021-03-06 21:21:56 +08:00 committed by wangkai
parent 7342095eb2
commit 9177c1ab58
2 changed files with 59 additions and 106 deletions

View File

@ -12,17 +12,53 @@ import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${basePackage}.util.object.ObjectUtils;
import ${PageResultClassName};
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*;
import static cn.hutool.core.util.RandomUtil.*; import static cn.hutool.core.util.RandomUtil.*;
import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*;
import static cn.iocoder.dashboard.util.AssertUtils.*; import static ${basePackage}.util.AssertUtils.*;
import static cn.iocoder.dashboard.util.RandomUtils.*; import static ${basePackage}.util.RandomUtils.*;
import static cn.iocoder.dashboard.util.date.DateUtils.*; import static ${basePackage}.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
## 字段模板
#macro(getPageCondition $VO)
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
o.set$JavaField(null);
#end
#end
});
${classNameVar}Mapper.insert(db${simpleClassName});
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
// 测试 ${column.javaField} 不匹配
${classNameVar}Mapper.insert(ObjectUtils.clone(db${simpleClassName}, o -> o.set$JavaField(null)));
#end
#end
// 准备参数
${table.className}${VO} reqVO = new ${table.className}${VO}();
#foreach ($column in $columns)
#if (${column.listOperation})
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况
reqVO.setBegin${JavaField}(null);
reqVO.setEnd${JavaField}(null);
#else
reqVO.set$JavaField(null);
#end
#end
#end
#end
/** /**
* {@link ${table.className}ServiceImpl} 的单元测试类 * {@link ${table.className}ServiceImpl} 的单元测试类
* *
@ -99,13 +135,27 @@ public class ${table.className}ServiceTest extends BaseSpringBootUnitTest {
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
} }
@Test @Test // TODO 请修改 null 为需要的值
public void testGet${simpleClassName}Page() { public void testGet${simpleClassName}Page() {
// mock 数据 #getPageCondition("PageReqVO")
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到
}); // 调用
${classNameVar}Mapper.insert(db${simpleClassName}); PageResult<${table.className}DO> pageResult = configService.getConfigPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0));
}
@Test // TODO 请修改 null 为需要的值
public void testGet${simpleClassName}List() {
#getPageCondition("ExportReqVO")
// 调用
PageResult<${table.className}DO> list = configService.getConfigPage(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(db${simpleClassName}, list.get(0));
} }
} }

View File

@ -1,97 +0,0 @@
package cn.iocoder.dashboard.modules.infra.service.job;
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO;
import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigUpdateReqVO;
import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO;
import cn.iocoder.dashboard.modules.infra.dal.mysql.config.InfConfigMapper;
import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl;
import org.junit.jupiter.api.Test;
import javax.annotation.Resource;
import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
import static cn.iocoder.dashboard.util.RandomUtils.randomLongId;
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* {@link InfConfigServiceImpl} 的单元测试类
*
* @author 芋艿
*/
public class InfConfigServiceTest extends BaseSpringBootUnitTest {
@Resource
private InfConfigServiceImpl configService;
@Resource
private InfConfigMapper configMapper;
@Test
public void testCreateConfig_success() {
// 准备参数
InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
// 调用
Long configId = configService.createConfig(reqVO);
// 断言
assertNotNull(configId);
// 校验记录的属性是否正确
InfConfigDO config = configMapper.selectById(configId);
assertPojoEquals(reqVO, config);
}
@Test
public void testUpdateConfig_success() {
// mock 数据
InfConfigDO dbConfig = randomPojo(InfConfigDO.class);
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
// 准备参数
InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> {
o.setId(dbConfig.getId()); // 设置更新的 ID
});
// 调用
configService.updateConfig(reqVO);
// 校验是否更新正确
InfConfigDO config = configMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, config);
}
@Test
public void testUpdateConfig_notExists() {
// 准备参数
InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> configService.updateConfig(reqVO), CONFIG_NOT_EXISTS);
}
@Test
public void testDeleteConfig_success() {
// mock 数据
InfConfigDO dbConfig = randomPojo(InfConfigDO.class);
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbConfig.getId();
// 调用
configService.deleteConfig(id);
// 校验数据不存在了
assertNull(configMapper.selectById(id));
}
@Test
public void testDeleteConfig_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS);
}
}