1. 调整 codegen 的模板

2. 编写 ut 的代码生成器模板
This commit is contained in:
YunaiV 2021-03-06 20:06:43 +08:00 committed by wangkai
parent a9b6d18da9
commit 7342095eb2
21 changed files with 270 additions and 48 deletions

View File

@ -53,9 +53,7 @@ public class ToolCodegenEngine {
* value生成的路径
*/
private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序
// Java
.put(javaTemplatePath("controller/controller"),
javaFilePath("controller/${table.businessName}/${table.className}Controller"))
// Java Main
.put(javaTemplatePath("controller/vo/baseVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}BaseVO"))
.put(javaTemplatePath("controller/vo/createReqVO"),
@ -70,6 +68,8 @@ public class ToolCodegenEngine {
javaFilePath("controller/${table.businessName}/vo/${table.className}ExportReqVO"))
.put(javaTemplatePath("controller/vo/excelVO"),
javaFilePath("controller/${table.businessName}/vo/${table.className}ExcelVO"))
.put(javaTemplatePath("controller/controller"),
javaFilePath("controller/${table.businessName}/${table.className}Controller"))
.put(javaTemplatePath("convert/convert"),
javaFilePath("convert/${table.businessName}/${table.className}Convert"))
.put(javaTemplatePath("dal/do"),
@ -78,10 +78,13 @@ public class ToolCodegenEngine {
javaFilePath("dal/mysql/${table.businessName}/${table.className}Mapper"))
.put(javaTemplatePath("enums/errorcode"),
javaFilePath("enums/${simpleModuleName_upperFirst}ErrorCodeConstants"))
.put(javaTemplatePath("service/service"),
javaFilePath("service/${table.businessName}/${table.className}Service"))
.put(javaTemplatePath("service/serviceImpl"),
javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl"))
.put(javaTemplatePath("service/service"),
javaFilePath("service/${table.businessName}/${table.className}Service"))
// Java Test
.put(javaTemplatePath("test/serviceTest"),
javaFilePath("service/${table.businessName}/${table.className}ServiceTest"))
// Vue
.put(vueTemplatePath("views/index.vue"),
vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue"))

View File

@ -154,7 +154,7 @@ yudao:
file:
base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/
codegen:
base-package: ${yudao.info.base-package}.modules
base-package: ${yudao.info.base-package}
db-schemas: ${spring.datasource.name}
xss:
enable: false

View File

@ -154,7 +154,7 @@ yudao:
file:
base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/
codegen:
base-package: ${yudao.info.base-package}.modules
base-package: ${yudao.info.base-package}
db-schemas: ${spring.datasource.name}
xss:
enable: false

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName};
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName};
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@ -22,10 +22,10 @@ import ${ExcelUtilsClassName};
import ${OperateLogClassName};
import static ${OperateTypeEnumClassName}.*;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service;
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}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.${table.className}Service;
@Api(tags = "${table.classComment}")
@RestController

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo;
package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo;
import lombok.*;
import java.util.*;

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.convert.${table.businessName};
package ${basePackage}.modules.${table.moduleName}.convert.${table.businessName};
import java.util.*;
@ -6,8 +6,8 @@ import ${PageResultClassName};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
/**
* ${table.classComment} Convert

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName};
package ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName};
import lombok.*;
import java.util.*;

View File

@ -1,13 +1,13 @@
package ${basePackage}.${table.moduleName}.dal.mysql.${table.businessName};
package ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName};
import java.util.*;
import ${PageResultClassName};
import ${QueryWrapperClassName};
import ${BaseMapperClassName};
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import org.apache.ibatis.annotations.Mapper;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
## 字段模板
#macro(listCondition)

View File

@ -1,9 +1,9 @@
package ${basePackage}.${table.moduleName}.service.${table.businessName};
package ${basePackage}.modules.${table.moduleName}.service.${table.businessName};
import java.util.*;
import javax.validation.*;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${PageResultClassName};
/**

View File

@ -1,4 +1,4 @@
package ${basePackage}.${table.moduleName}.service.${table.businessName}.impl;
package ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -6,17 +6,17 @@ import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
import ${PageResultClassName};
import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service;
import ${basePackage}.modules.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.${table.className}Service;
import ${ServiceExceptionUtilClassName};
import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*;
import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*;
/**
* ${table.classComment} Service 实现类

View File

@ -0,0 +1,111 @@
package ${basePackage}.modules.${table.moduleName}.service.${table.businessName};
import ${basePackage}.BaseSpringBootUnitTest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import javax.annotation.Resource;
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.impl.${table.className}ServiceImpl;
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.mysql.${table.businessName}.${table.className}Mapper;
import javax.annotation.Resource;
import static cn.hutool.core.util.RandomUtil.*;
import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*;
import static cn.iocoder.dashboard.util.AssertUtils.*;
import static cn.iocoder.dashboard.util.RandomUtils.*;
import static cn.iocoder.dashboard.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* {@link ${table.className}ServiceImpl} 的单元测试类
*
* @author ${table.author}
*/
public class ${table.className}ServiceTest extends BaseSpringBootUnitTest {
@Resource
private ${table.className}ServiceImpl ${classNameVar}Service;
@Resource
private ${table.className}Mapper ${classNameVar}Mapper;
@Test
public void testCreate${simpleClassName}_success() {
// 准备参数
${table.className}CreateReqVO reqVO = randomPojo(${table.className}CreateReqVO.class);
// 调用
Long ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(reqVO);
// 断言
assertNotNull(${classNameVar}Id);
// 校验记录的属性是否正确
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id);
assertPojoEquals(reqVO, ${classNameVar});
}
@Test
public void testUpdate${simpleClassName}_success() {
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
// 准备参数
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class, o -> {
o.setId(db${simpleClassName}.getId()); // 设置更新的 ID
});
// 调用
${classNameVar}Service.update${simpleClassName}(reqVO);
// 校验是否更新正确
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, ${classNameVar});
}
@Test
public void testUpdate${simpleClassName}_notExists() {
// 准备参数
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(reqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
@Test
public void testDelete${simpleClassName}_success() {
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = db${simpleClassName}.getId();
// 调用
${classNameVar}Service.delete${simpleClassName}(id);
// 校验数据不存在了
assertNull(configMapper.selectById(id));
}
@Test
public void testDelete${simpleClassName}_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
@Test
public void testGet${simpleClassName}Page() {
// mock 数据
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到
});
${classNameVar}Mapper.insert(db${simpleClassName});
}
}

View File

@ -20,24 +20,26 @@ import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
import static cn.iocoder.dashboard.util.AssertUtils.*;
import static cn.iocoder.dashboard.util.RandomUtils.*;
import static cn.iocoder.dashboard.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
/**
* {@link InfConfigServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
public class InfConfigServiceTest extends BaseSpringBootUnitTest {
@Resource
private InfConfigServiceImpl configService;
@Resource
private InfConfigMapper configMapper;
@MockBean
private InfConfigProducer configProducer;
@ -213,6 +215,15 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
}
@Test
public void testDeleteConfig_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS);
}
// ========== 随机对象 ==========
@SafeVarargs

View File

@ -0,0 +1,97 @@
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);
}
}

View File

@ -61,7 +61,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest {
@Test
public void testMockLogin_success() {
// 准备参数
Long userId = randomLong();
Long userId = randomLongId();
// mock 方法 01
SysUserDO user = randomUserDO(o -> o.setId(userId));
when(userService.getUser(eq(userId))).thenReturn(user);
@ -80,7 +80,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest {
@Test
public void testMockLogin_userNotFound() {
// 准备参数
Long userId = randomLong();
Long userId = randomLongId();
// mock 方法
// 调用, 并断言异常

View File

@ -46,7 +46,7 @@ public class RandomUtils {
return RandomUtil.randomString(RANDOM_STRING_LENGTH);
}
public static Long randomLong() {
public static Long randomLongId() {
return RandomUtil.randomLong(0, Long.MAX_VALUE);
}