Commit aaf762e74f6cbda49ed2543c8b76355ae10bba6d
1 parent
ea882102
1. 调整 codegen 的模板
2. 编写 ut 的代码生成器模板
Showing
21 changed files
with
270 additions
and
48 deletions
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java
| @@ -53,9 +53,7 @@ public class ToolCodegenEngine { | @@ -53,9 +53,7 @@ public class ToolCodegenEngine { | ||
| 53 | * value:生成的路径 | 53 | * value:生成的路径 |
| 54 | */ | 54 | */ |
| 55 | private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 | 55 | private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 |
| 56 | - // Java | ||
| 57 | - .put(javaTemplatePath("controller/controller"), | ||
| 58 | - javaFilePath("controller/${table.businessName}/${table.className}Controller")) | 56 | + // Java Main |
| 59 | .put(javaTemplatePath("controller/vo/baseVO"), | 57 | .put(javaTemplatePath("controller/vo/baseVO"), |
| 60 | javaFilePath("controller/${table.businessName}/vo/${table.className}BaseVO")) | 58 | javaFilePath("controller/${table.businessName}/vo/${table.className}BaseVO")) |
| 61 | .put(javaTemplatePath("controller/vo/createReqVO"), | 59 | .put(javaTemplatePath("controller/vo/createReqVO"), |
| @@ -70,6 +68,8 @@ public class ToolCodegenEngine { | @@ -70,6 +68,8 @@ public class ToolCodegenEngine { | ||
| 70 | javaFilePath("controller/${table.businessName}/vo/${table.className}ExportReqVO")) | 68 | javaFilePath("controller/${table.businessName}/vo/${table.className}ExportReqVO")) |
| 71 | .put(javaTemplatePath("controller/vo/excelVO"), | 69 | .put(javaTemplatePath("controller/vo/excelVO"), |
| 72 | javaFilePath("controller/${table.businessName}/vo/${table.className}ExcelVO")) | 70 | javaFilePath("controller/${table.businessName}/vo/${table.className}ExcelVO")) |
| 71 | + .put(javaTemplatePath("controller/controller"), | ||
| 72 | + javaFilePath("controller/${table.businessName}/${table.className}Controller")) | ||
| 73 | .put(javaTemplatePath("convert/convert"), | 73 | .put(javaTemplatePath("convert/convert"), |
| 74 | javaFilePath("convert/${table.businessName}/${table.className}Convert")) | 74 | javaFilePath("convert/${table.businessName}/${table.className}Convert")) |
| 75 | .put(javaTemplatePath("dal/do"), | 75 | .put(javaTemplatePath("dal/do"), |
| @@ -78,10 +78,13 @@ public class ToolCodegenEngine { | @@ -78,10 +78,13 @@ public class ToolCodegenEngine { | ||
| 78 | javaFilePath("dal/mysql/${table.businessName}/${table.className}Mapper")) | 78 | javaFilePath("dal/mysql/${table.businessName}/${table.className}Mapper")) |
| 79 | .put(javaTemplatePath("enums/errorcode"), | 79 | .put(javaTemplatePath("enums/errorcode"), |
| 80 | javaFilePath("enums/${simpleModuleName_upperFirst}ErrorCodeConstants")) | 80 | javaFilePath("enums/${simpleModuleName_upperFirst}ErrorCodeConstants")) |
| 81 | - .put(javaTemplatePath("service/service"), | ||
| 82 | - javaFilePath("service/${table.businessName}/${table.className}Service")) | ||
| 83 | .put(javaTemplatePath("service/serviceImpl"), | 81 | .put(javaTemplatePath("service/serviceImpl"), |
| 84 | javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl")) | 82 | javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl")) |
| 83 | + .put(javaTemplatePath("service/service"), | ||
| 84 | + javaFilePath("service/${table.businessName}/${table.className}Service")) | ||
| 85 | + // Java Test | ||
| 86 | + .put(javaTemplatePath("test/serviceTest"), | ||
| 87 | + javaFilePath("service/${table.businessName}/${table.className}ServiceTest")) | ||
| 85 | // Vue | 88 | // Vue |
| 86 | .put(vueTemplatePath("views/index.vue"), | 89 | .put(vueTemplatePath("views/index.vue"), |
| 87 | vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue")) | 90 | vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue")) |
src/main/resources/application-dev.yaml
| @@ -154,7 +154,7 @@ yudao: | @@ -154,7 +154,7 @@ yudao: | ||
| 154 | file: | 154 | file: |
| 155 | base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/ | 155 | base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/ |
| 156 | codegen: | 156 | codegen: |
| 157 | - base-package: ${yudao.info.base-package}.modules | 157 | + base-package: ${yudao.info.base-package} |
| 158 | db-schemas: ${spring.datasource.name} | 158 | db-schemas: ${spring.datasource.name} |
| 159 | xss: | 159 | xss: |
| 160 | enable: false | 160 | enable: false |
src/main/resources/application-local.yaml
| @@ -154,7 +154,7 @@ yudao: | @@ -154,7 +154,7 @@ yudao: | ||
| 154 | file: | 154 | file: |
| 155 | base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/ | 155 | base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/ |
| 156 | codegen: | 156 | codegen: |
| 157 | - base-package: ${yudao.info.base-package}.modules | 157 | + base-package: ${yudao.info.base-package} |
| 158 | db-schemas: ${spring.datasource.name} | 158 | db-schemas: ${spring.datasource.name} |
| 159 | xss: | 159 | xss: |
| 160 | enable: false | 160 | enable: false |
src/main/resources/codegen/java/controller/controller.vm
| 1 | -package ${basePackage}.${table.moduleName}.controller.${table.businessName}; | 1 | +package ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}; |
| 2 | 2 | ||
| 3 | import org.springframework.web.bind.annotation.*; | 3 | import org.springframework.web.bind.annotation.*; |
| 4 | import javax.annotation.Resource; | 4 | import javax.annotation.Resource; |
| @@ -22,10 +22,10 @@ import ${ExcelUtilsClassName}; | @@ -22,10 +22,10 @@ import ${ExcelUtilsClassName}; | ||
| 22 | import ${OperateLogClassName}; | 22 | import ${OperateLogClassName}; |
| 23 | import static ${OperateTypeEnumClassName}.*; | 23 | import static ${OperateTypeEnumClassName}.*; |
| 24 | 24 | ||
| 25 | -import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | ||
| 26 | -import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | ||
| 27 | -import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; | ||
| 28 | -import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service; | 25 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 26 | +import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | ||
| 27 | +import ${basePackage}.modules.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; | ||
| 28 | +import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.${table.className}Service; | ||
| 29 | 29 | ||
| 30 | @Api(tags = "${table.classComment}") | 30 | @Api(tags = "${table.classComment}") |
| 31 | @RestController | 31 | @RestController |
src/main/resources/codegen/java/controller/vo/baseVO.vm
src/main/resources/codegen/java/controller/vo/createReqVO.vm
src/main/resources/codegen/java/controller/vo/excelVO.vm
src/main/resources/codegen/java/controller/vo/exportReqVO.vm
src/main/resources/codegen/java/controller/vo/pageReqVO.vm
src/main/resources/codegen/java/controller/vo/respVO.vm
src/main/resources/codegen/java/controller/vo/updateReqVO.vm
src/main/resources/codegen/java/convert/convert.vm
| 1 | -package ${basePackage}.${table.moduleName}.convert.${table.businessName}; | 1 | +package ${basePackage}.modules.${table.moduleName}.convert.${table.businessName}; |
| 2 | 2 | ||
| 3 | import java.util.*; | 3 | import java.util.*; |
| 4 | 4 | ||
| @@ -6,8 +6,8 @@ import ${PageResultClassName}; | @@ -6,8 +6,8 @@ import ${PageResultClassName}; | ||
| 6 | 6 | ||
| 7 | import org.mapstruct.Mapper; | 7 | import org.mapstruct.Mapper; |
| 8 | import org.mapstruct.factory.Mappers; | 8 | import org.mapstruct.factory.Mappers; |
| 9 | -import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | ||
| 10 | -import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | 9 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 10 | +import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | ||
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | * ${table.classComment} Convert | 13 | * ${table.classComment} Convert |
src/main/resources/codegen/java/dal/do.vm
src/main/resources/codegen/java/dal/mapper.vm
| 1 | -package ${basePackage}.${table.moduleName}.dal.mysql.${table.businessName}; | 1 | +package ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}; |
| 2 | 2 | ||
| 3 | import java.util.*; | 3 | import java.util.*; |
| 4 | 4 | ||
| 5 | import ${PageResultClassName}; | 5 | import ${PageResultClassName}; |
| 6 | import ${QueryWrapperClassName}; | 6 | import ${QueryWrapperClassName}; |
| 7 | import ${BaseMapperClassName}; | 7 | import ${BaseMapperClassName}; |
| 8 | -import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | 8 | +import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
| 9 | import org.apache.ibatis.annotations.Mapper; | 9 | import org.apache.ibatis.annotations.Mapper; |
| 10 | -import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | 10 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 11 | 11 | ||
| 12 | ## 字段模板 | 12 | ## 字段模板 |
| 13 | #macro(listCondition) | 13 | #macro(listCondition) |
src/main/resources/codegen/java/service/service.vm
| 1 | -package ${basePackage}.${table.moduleName}.service.${table.businessName}; | 1 | +package ${basePackage}.modules.${table.moduleName}.service.${table.businessName}; |
| 2 | 2 | ||
| 3 | import java.util.*; | 3 | import java.util.*; |
| 4 | import javax.validation.*; | 4 | import javax.validation.*; |
| 5 | -import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | ||
| 6 | -import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | 5 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 6 | +import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | ||
| 7 | import ${PageResultClassName}; | 7 | import ${PageResultClassName}; |
| 8 | 8 | ||
| 9 | /** | 9 | /** |
src/main/resources/codegen/java/service/serviceImpl.vm
| 1 | -package ${basePackage}.${table.moduleName}.service.${table.businessName}.impl; | 1 | +package ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.impl; |
| 2 | 2 | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | 3 | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | import org.springframework.stereotype.Service; | 4 | import org.springframework.stereotype.Service; |
| @@ -6,17 +6,17 @@ import javax.annotation.Resource; | @@ -6,17 +6,17 @@ import javax.annotation.Resource; | ||
| 6 | import org.springframework.validation.annotation.Validated; | 6 | import org.springframework.validation.annotation.Validated; |
| 7 | 7 | ||
| 8 | import java.util.*; | 8 | import java.util.*; |
| 9 | -import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | ||
| 10 | -import ${basePackage}.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | 9 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; |
| 10 | +import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; | ||
| 11 | import ${PageResultClassName}; | 11 | import ${PageResultClassName}; |
| 12 | 12 | ||
| 13 | -import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; | ||
| 14 | -import ${basePackage}.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; | ||
| 15 | -import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service; | 13 | +import ${basePackage}.modules.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; |
| 14 | +import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; | ||
| 15 | +import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.${table.className}Service; | ||
| 16 | 16 | ||
| 17 | import ${ServiceExceptionUtilClassName}; | 17 | import ${ServiceExceptionUtilClassName}; |
| 18 | 18 | ||
| 19 | -import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; | 19 | +import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; |
| 20 | 20 | ||
| 21 | /** | 21 | /** |
| 22 | * ${table.classComment} Service 实现类 | 22 | * ${table.classComment} Service 实现类 |
src/main/resources/codegen/java/test/serviceTest.vm
0 → 100644
| 1 | +package ${basePackage}.modules.${table.moduleName}.service.${table.businessName}; | ||
| 2 | + | ||
| 3 | +import ${basePackage}.BaseSpringBootUnitTest; | ||
| 4 | + | ||
| 5 | +import org.junit.jupiter.api.Test; | ||
| 6 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 7 | + | ||
| 8 | +import javax.annotation.Resource; | ||
| 9 | + | ||
| 10 | +import cn.iocoder.dashboard.BaseSpringBootUnitTest; | ||
| 11 | +import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.impl.${table.className}ServiceImpl; | ||
| 12 | +import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; | ||
| 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; | ||
| 15 | + | ||
| 16 | +import javax.annotation.Resource; | ||
| 17 | + | ||
| 18 | +import static cn.hutool.core.util.RandomUtil.*; | ||
| 19 | +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 org.junit.jupiter.api.Assertions.*; | ||
| 24 | +import static org.mockito.Mockito.*; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | +* {@link ${table.className}ServiceImpl} 的单元测试类 | ||
| 28 | +* | ||
| 29 | +* @author ${table.author} | ||
| 30 | +*/ | ||
| 31 | +public class ${table.className}ServiceTest extends BaseSpringBootUnitTest { | ||
| 32 | + | ||
| 33 | + @Resource | ||
| 34 | + private ${table.className}ServiceImpl ${classNameVar}Service; | ||
| 35 | + | ||
| 36 | + @Resource | ||
| 37 | + private ${table.className}Mapper ${classNameVar}Mapper; | ||
| 38 | + | ||
| 39 | + @Test | ||
| 40 | + public void testCreate${simpleClassName}_success() { | ||
| 41 | + // 准备参数 | ||
| 42 | + ${table.className}CreateReqVO reqVO = randomPojo(${table.className}CreateReqVO.class); | ||
| 43 | + | ||
| 44 | + // 调用 | ||
| 45 | + Long ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(reqVO); | ||
| 46 | + // 断言 | ||
| 47 | + assertNotNull(${classNameVar}Id); | ||
| 48 | + // 校验记录的属性是否正确 | ||
| 49 | + ${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id); | ||
| 50 | + assertPojoEquals(reqVO, ${classNameVar}); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Test | ||
| 54 | + public void testUpdate${simpleClassName}_success() { | ||
| 55 | + // mock 数据 | ||
| 56 | + ${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); | ||
| 57 | + ${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 | ||
| 58 | + // 准备参数 | ||
| 59 | + ${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class, o -> { | ||
| 60 | + o.setId(db${simpleClassName}.getId()); // 设置更新的 ID | ||
| 61 | + }); | ||
| 62 | + | ||
| 63 | + // 调用 | ||
| 64 | + ${classNameVar}Service.update${simpleClassName}(reqVO); | ||
| 65 | + // 校验是否更新正确 | ||
| 66 | + ${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(reqVO.getId()); // 获取最新的 | ||
| 67 | + assertPojoEquals(reqVO, ${classNameVar}); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Test | ||
| 71 | + public void testUpdate${simpleClassName}_notExists() { | ||
| 72 | + // 准备参数 | ||
| 73 | + ${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class); | ||
| 74 | + | ||
| 75 | + // 调用, 并断言异常 | ||
| 76 | + assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(reqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Test | ||
| 80 | + public void testDelete${simpleClassName}_success() { | ||
| 81 | + // mock 数据 | ||
| 82 | + ${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); | ||
| 83 | + ${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 | ||
| 84 | + // 准备参数 | ||
| 85 | + Long id = db${simpleClassName}.getId(); | ||
| 86 | + | ||
| 87 | + // 调用 | ||
| 88 | + ${classNameVar}Service.delete${simpleClassName}(id); | ||
| 89 | + // 校验数据不存在了 | ||
| 90 | + assertNull(configMapper.selectById(id)); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + @Test | ||
| 94 | + public void testDelete${simpleClassName}_notExists() { | ||
| 95 | + // 准备参数 | ||
| 96 | + Long id = randomLongId(); | ||
| 97 | + | ||
| 98 | + // 调用, 并断言异常 | ||
| 99 | + assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @Test | ||
| 103 | + public void testGet${simpleClassName}Page() { | ||
| 104 | + // mock 数据 | ||
| 105 | + ${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到 | ||
| 106 | + | ||
| 107 | + }); | ||
| 108 | + ${classNameVar}Mapper.insert(db${simpleClassName}); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | +} |
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java renamed to src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java
| @@ -20,24 +20,26 @@ import javax.annotation.Resource; | @@ -20,24 +20,26 @@ import javax.annotation.Resource; | ||
| 20 | import java.util.List; | 20 | import java.util.List; |
| 21 | import java.util.function.Consumer; | 21 | import java.util.function.Consumer; |
| 22 | 22 | ||
| 23 | -import static cn.hutool.core.util.RandomUtil.randomEle; | 23 | +import static cn.hutool.core.util.RandomUtil.*; |
| 24 | import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*; | 24 | import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*; |
| 25 | -import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||
| 26 | -import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | ||
| 27 | -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||
| 28 | -import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; | 25 | +import static cn.iocoder.dashboard.util.AssertUtils.*; |
| 26 | +import static cn.iocoder.dashboard.util.RandomUtils.*; | ||
| 27 | +import static cn.iocoder.dashboard.util.date.DateUtils.*; | ||
| 29 | import static org.junit.jupiter.api.Assertions.*; | 28 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | -import static org.mockito.Mockito.times; | ||
| 31 | -import static org.mockito.Mockito.verify; | 29 | +import static org.mockito.Mockito.*; |
| 32 | 30 | ||
| 33 | -public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | 31 | +/** |
| 32 | + * {@link InfConfigServiceImpl} 的单元测试类 | ||
| 33 | + * | ||
| 34 | + * @author 芋道源码 | ||
| 35 | + */ | ||
| 36 | +public class InfConfigServiceTest extends BaseSpringBootUnitTest { | ||
| 34 | 37 | ||
| 35 | @Resource | 38 | @Resource |
| 36 | private InfConfigServiceImpl configService; | 39 | private InfConfigServiceImpl configService; |
| 37 | 40 | ||
| 38 | @Resource | 41 | @Resource |
| 39 | private InfConfigMapper configMapper; | 42 | private InfConfigMapper configMapper; |
| 40 | - | ||
| 41 | @MockBean | 43 | @MockBean |
| 42 | private InfConfigProducer configProducer; | 44 | private InfConfigProducer configProducer; |
| 43 | 45 | ||
| @@ -213,6 +215,15 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | @@ -213,6 +215,15 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { | ||
| 213 | assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE); | 215 | assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE); |
| 214 | } | 216 | } |
| 215 | 217 | ||
| 218 | + @Test | ||
| 219 | + public void testDeleteConfig_notExists() { | ||
| 220 | + // 准备参数 | ||
| 221 | + Long id = randomLongId(); | ||
| 222 | + | ||
| 223 | + // 调用, 并断言异常 | ||
| 224 | + assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS); | ||
| 225 | + } | ||
| 226 | + | ||
| 216 | // ========== 随机对象 ========== | 227 | // ========== 随机对象 ========== |
| 217 | 228 | ||
| 218 | @SafeVarargs | 229 | @SafeVarargs |
src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfConfigServiceTest.java
0 → 100644
| 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 | +} |
src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java
| @@ -61,7 +61,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { | @@ -61,7 +61,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { | ||
| 61 | @Test | 61 | @Test |
| 62 | public void testMockLogin_success() { | 62 | public void testMockLogin_success() { |
| 63 | // 准备参数 | 63 | // 准备参数 |
| 64 | - Long userId = randomLong(); | 64 | + Long userId = randomLongId(); |
| 65 | // mock 方法 01 | 65 | // mock 方法 01 |
| 66 | SysUserDO user = randomUserDO(o -> o.setId(userId)); | 66 | SysUserDO user = randomUserDO(o -> o.setId(userId)); |
| 67 | when(userService.getUser(eq(userId))).thenReturn(user); | 67 | when(userService.getUser(eq(userId))).thenReturn(user); |
| @@ -80,7 +80,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { | @@ -80,7 +80,7 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { | ||
| 80 | @Test | 80 | @Test |
| 81 | public void testMockLogin_userNotFound() { | 81 | public void testMockLogin_userNotFound() { |
| 82 | // 准备参数 | 82 | // 准备参数 |
| 83 | - Long userId = randomLong(); | 83 | + Long userId = randomLongId(); |
| 84 | // mock 方法 | 84 | // mock 方法 |
| 85 | 85 | ||
| 86 | // 调用, 并断言异常 | 86 | // 调用, 并断言异常 |
src/test/java/cn/iocoder/dashboard/util/RandomUtils.java
| @@ -46,7 +46,7 @@ public class RandomUtils { | @@ -46,7 +46,7 @@ public class RandomUtils { | ||
| 46 | return RandomUtil.randomString(RANDOM_STRING_LENGTH); | 46 | return RandomUtil.randomString(RANDOM_STRING_LENGTH); |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | - public static Long randomLong() { | 49 | + public static Long randomLongId() { |
| 50 | return RandomUtil.randomLong(0, Long.MAX_VALUE); | 50 | return RandomUtil.randomLong(0, Long.MAX_VALUE); |
| 51 | } | 51 | } |
| 52 | 52 |