diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java index 927c9775a..7ccf26001 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java @@ -53,9 +53,7 @@ public class ToolCodegenEngine { * value:生成的路径 */ private static final Map TEMPLATES = MapUtil.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")) diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 6e4dfb3de..dbf2ad53b 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -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 diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 5019cd18d..537156cf7 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -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 diff --git a/src/main/resources/codegen/java/controller/controller.vm b/src/main/resources/codegen/java/controller/controller.vm index 9cb416928..664d7c55b 100644 --- a/src/main/resources/codegen/java/controller/controller.vm +++ b/src/main/resources/codegen/java/controller/controller.vm @@ -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 diff --git a/src/main/resources/codegen/java/controller/vo/baseVO.vm b/src/main/resources/codegen/java/controller/vo/baseVO.vm index 8e8586550..705869a8d 100644 --- a/src/main/resources/codegen/java/controller/vo/baseVO.vm +++ b/src/main/resources/codegen/java/controller/vo/baseVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/createReqVO.vm b/src/main/resources/codegen/java/controller/vo/createReqVO.vm index 3ef3618cd..cfb288241 100644 --- a/src/main/resources/codegen/java/controller/vo/createReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/createReqVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/excelVO.vm b/src/main/resources/codegen/java/controller/vo/excelVO.vm index 8eb035027..29f777190 100644 --- a/src/main/resources/codegen/java/controller/vo/excelVO.vm +++ b/src/main/resources/codegen/java/controller/vo/excelVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/exportReqVO.vm b/src/main/resources/codegen/java/controller/vo/exportReqVO.vm index 6a5dbc0e6..cd0e340e8 100644 --- a/src/main/resources/codegen/java/controller/vo/exportReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/exportReqVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/pageReqVO.vm b/src/main/resources/codegen/java/controller/vo/pageReqVO.vm index 71630742e..0ac69e94d 100644 --- a/src/main/resources/codegen/java/controller/vo/pageReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/pageReqVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/respVO.vm b/src/main/resources/codegen/java/controller/vo/respVO.vm index 9176c073f..a100dd16c 100644 --- a/src/main/resources/codegen/java/controller/vo/respVO.vm +++ b/src/main/resources/codegen/java/controller/vo/respVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/controller/vo/updateReqVO.vm b/src/main/resources/codegen/java/controller/vo/updateReqVO.vm index 173b8dbcf..a05ae8d66 100644 --- a/src/main/resources/codegen/java/controller/vo/updateReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/updateReqVO.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/convert/convert.vm b/src/main/resources/codegen/java/convert/convert.vm index 0ec103181..4302bd2c5 100644 --- a/src/main/resources/codegen/java/convert/convert.vm +++ b/src/main/resources/codegen/java/convert/convert.vm @@ -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 diff --git a/src/main/resources/codegen/java/dal/do.vm b/src/main/resources/codegen/java/dal/do.vm index b70d01d18..8f10fd5e4 100644 --- a/src/main/resources/codegen/java/dal/do.vm +++ b/src/main/resources/codegen/java/dal/do.vm @@ -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.*; diff --git a/src/main/resources/codegen/java/dal/mapper.vm b/src/main/resources/codegen/java/dal/mapper.vm index cc1c2ae7e..2452ca8f6 100644 --- a/src/main/resources/codegen/java/dal/mapper.vm +++ b/src/main/resources/codegen/java/dal/mapper.vm @@ -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) diff --git a/src/main/resources/codegen/java/service/service.vm b/src/main/resources/codegen/java/service/service.vm index 31cba1cca..04499314e 100644 --- a/src/main/resources/codegen/java/service/service.vm +++ b/src/main/resources/codegen/java/service/service.vm @@ -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}; /** diff --git a/src/main/resources/codegen/java/service/serviceImpl.vm b/src/main/resources/codegen/java/service/serviceImpl.vm index 7bd61c8c9..0889f0ce0 100644 --- a/src/main/resources/codegen/java/service/serviceImpl.vm +++ b/src/main/resources/codegen/java/service/serviceImpl.vm @@ -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 实现类 diff --git a/src/main/resources/codegen/java/test/serviceTest.vm b/src/main/resources/codegen/java/test/serviceTest.vm new file mode 100644 index 000000000..8713db421 --- /dev/null +++ b/src/main/resources/codegen/java/test/serviceTest.vm @@ -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}); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java similarity index 92% rename from src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java rename to src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java index d56aabf69..6f95f5ad0 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java @@ -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 diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfConfigServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfConfigServiceTest.java new file mode 100644 index 000000000..d5fdf5cb9 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfConfigServiceTest.java @@ -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); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java index 5e79a0cf5..5d9ac58a7 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java @@ -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 方法 // 调用, 并断言异常 diff --git a/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java index 36d58271f..572668647 100644 --- a/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java +++ b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java @@ -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); }