完善 DictTypeServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 07:45:47 +08:00
parent 30f097ed26
commit 0a2fbae9d8
2 changed files with 104 additions and 70 deletions

View File

@ -57,7 +57,8 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override @Override
public Long createDictType(DictTypeCreateReqVO reqVO) { public Long createDictType(DictTypeCreateReqVO reqVO) {
// 校验正确性 // 校验正确性
checkCreateOrUpdate(null, reqVO.getName(), reqVO.getType()); validateDictTypeForCreateOrUpdate(null, reqVO.getName(), reqVO.getType());
// 插入字典类型 // 插入字典类型
DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO) DictTypeDO dictType = DictTypeConvert.INSTANCE.convert(reqVO)
.setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引避免 null .setDeletedTime(LocalDateTimeUtils.EMPTY); // 唯一索引避免 null
@ -68,7 +69,8 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override @Override
public void updateDictType(DictTypeUpdateReqVO reqVO) { public void updateDictType(DictTypeUpdateReqVO reqVO) {
// 校验正确性 // 校验正确性
checkCreateOrUpdate(reqVO.getId(), reqVO.getName(), null); validateDictTypeForCreateOrUpdate(reqVO.getId(), reqVO.getName(), null);
// 更新字典类型 // 更新字典类型
DictTypeDO updateObj = DictTypeConvert.INSTANCE.convert(reqVO); DictTypeDO updateObj = DictTypeConvert.INSTANCE.convert(reqVO);
dictTypeMapper.updateById(updateObj); dictTypeMapper.updateById(updateObj);
@ -77,7 +79,7 @@ public class DictTypeServiceImpl implements DictTypeService {
@Override @Override
public void deleteDictType(Long id) { public void deleteDictType(Long id) {
// 校验是否存在 // 校验是否存在
DictTypeDO dictType = checkDictTypeExists(id); DictTypeDO dictType = validateDictTypeExists(id);
// 校验是否有字典数据 // 校验是否有字典数据
if (dictDataService.countByDictType(dictType.getType()) > 0) { if (dictDataService.countByDictType(dictType.getType()) > 0) {
throw exception(DICT_TYPE_HAS_CHILDREN); throw exception(DICT_TYPE_HAS_CHILDREN);
@ -91,17 +93,17 @@ public class DictTypeServiceImpl implements DictTypeService {
return dictTypeMapper.selectList(); return dictTypeMapper.selectList();
} }
private void checkCreateOrUpdate(Long id, String name, String type) { private void validateDictTypeForCreateOrUpdate(Long id, String name, String type) {
// 校验自己存在 // 校验自己存在
checkDictTypeExists(id); validateDictTypeExists(id);
// 校验字典类型的名字的唯一性 // 校验字典类型的名字的唯一性
checkDictTypeNameUnique(id, name); validateDictTypeNameUnique(id, name);
// 校验字典类型的类型的唯一性 // 校验字典类型的类型的唯一性
checkDictTypeUnique(id, type); validateDictTypeUnique(id, type);
} }
@VisibleForTesting @VisibleForTesting
public void checkDictTypeNameUnique(Long id, String name) { void validateDictTypeNameUnique(Long id, String name) {
DictTypeDO dictType = dictTypeMapper.selectByName(name); DictTypeDO dictType = dictTypeMapper.selectByName(name);
if (dictType == null) { if (dictType == null) {
return; return;
@ -116,7 +118,7 @@ public class DictTypeServiceImpl implements DictTypeService {
} }
@VisibleForTesting @VisibleForTesting
public void checkDictTypeUnique(Long id, String type) { void validateDictTypeUnique(Long id, String type) {
if (StrUtil.isEmpty(type)) { if (StrUtil.isEmpty(type)) {
return; return;
} }
@ -134,7 +136,7 @@ public class DictTypeServiceImpl implements DictTypeService {
} }
@VisibleForTesting @VisibleForTesting
public DictTypeDO checkDictTypeExists(Long id) { DictTypeDO validateDictTypeExists(Long id) {
if (id == null) { if (id == null) {
return null; return null;
} }

View File

@ -2,36 +2,36 @@ package cn.iocoder.yudao.module.system.service.dict;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO; import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
import cn.iocoder.yudao.module.system.dal.mysql.dict.DictTypeMapper; import cn.iocoder.yudao.module.system.dal.mysql.dict.DictTypeMapper;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildLocalDateTime;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@Import(DictTypeServiceImpl.class) @Import(DictTypeServiceImpl.class)
public class DictTypeServiceTest extends BaseDbUnitTest { public class DictTypeServiceImplTest extends BaseDbUnitTest {
@Resource @Resource
private DictTypeServiceImpl dictTypeService; private DictTypeServiceImpl dictTypeService;
@ -48,23 +48,23 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
o.setName("yunai"); o.setName("yunai");
o.setType("芋艿"); o.setType("芋艿");
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(buildLocalDateTime(2021, 1, 15)); o.setCreateTime(buildTime(2021, 1, 15));
}); });
dictTypeMapper.insert(dbDictType); dictTypeMapper.insert(dbDictType);
// 测试 name 不匹配 // 测试 name 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
// 测试 type 不匹配 // 测试 type 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
// 测试 status 不匹配 // 测试 status 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildLocalDateTime(2021, 1, 1)))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
// 准备参数 // 准备参数
DictTypePageReqVO reqVO = new DictTypePageReqVO(); DictTypePageReqVO reqVO = new DictTypePageReqVO();
reqVO.setName("nai"); reqVO.setName("nai");
reqVO.setType(""); reqVO.setType("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2021, 1, 10),buildLocalDateTime(2021, 1, 20)})); reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20));
// 调用 // 调用
PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO); PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO);
@ -75,29 +75,29 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
} }
@Test @Test
public void testGetDictTypeList() { public void testGetDictTypeList_export() {
// mock 数据 // mock 数据
DictTypeDO dbDictType = randomPojo(DictTypeDO.class, o -> { // 等会查询到 DictTypeDO dbDictType = randomPojo(DictTypeDO.class, o -> { // 等会查询到
o.setName("yunai"); o.setName("yunai");
o.setType("芋艿"); o.setType("芋艿");
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(buildLocalDateTime(2021, 1, 15)); o.setCreateTime(buildTime(2021, 1, 15));
}); });
dictTypeMapper.insert(dbDictType); dictTypeMapper.insert(dbDictType);
// 测试 name 不匹配 // 测试 name 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou")));
// 测试 type 不匹配 // 测试 type 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆")));
// 测试 status 不匹配 // 测试 status 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildLocalDateTime(2021, 1, 1)))); dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
// 准备参数 // 准备参数
DictTypeExportReqVO reqVO = new DictTypeExportReqVO(); DictTypeExportReqVO reqVO = new DictTypeExportReqVO();
reqVO.setName("nai"); reqVO.setName("nai");
reqVO.setType(""); reqVO.setType("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2021, 1, 10),buildLocalDateTime(2021, 1, 20)})); reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20));
// 调用 // 调用
List<DictTypeDO> list = dictTypeService.getDictTypeList(reqVO); List<DictTypeDO> list = dictTypeService.getDictTypeList(reqVO);
@ -107,7 +107,22 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
} }
@Test @Test
public void testGetDictType() { public void testGetDictType_id() {
// mock 数据
DictTypeDO dbDictType = randomDictTypeDO();
dictTypeMapper.insert(dbDictType);
// 准备参数
Long id = dbDictType.getId();
// 调用
DictTypeDO dictType = dictTypeService.getDictType(id);
// 断言
assertNotNull(dictType);
assertPojoEquals(dbDictType, dictType);
}
@Test
public void testGetDictType_type() {
// mock 数据 // mock 数据
DictTypeDO dbDictType = randomDictTypeDO(); DictTypeDO dbDictType = randomDictTypeDO();
dictTypeMapper.insert(dbDictType); dictTypeMapper.insert(dbDictType);
@ -183,40 +198,57 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
} }
@Test @Test
public void testCheckDictDataExists_success() { public void testGetDictTypeList() {
// 准备参数
DictTypeDO dictTypeDO01 = randomDictTypeDO();
dictTypeMapper.insert(dictTypeDO01);
DictTypeDO dictTypeDO02 = randomDictTypeDO();
dictTypeMapper.insert(dictTypeDO02);
// mock 方法
// 调用
List<DictTypeDO> dictTypeDOList = dictTypeService.getDictTypeList();
// 断言
assertEquals(2, dictTypeDOList.size());
assertPojoEquals(dictTypeDO01, dictTypeDOList.get(0));
assertPojoEquals(dictTypeDO02, dictTypeDOList.get(1));
}
@Test
public void testValidateDictDataExists_success() {
// mock 数据 // mock 数据
DictTypeDO dbDictType = randomDictTypeDO(); DictTypeDO dbDictType = randomDictTypeDO();
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据
// 调用成功 // 调用成功
dictTypeService.checkDictTypeExists(dbDictType.getId()); dictTypeService.validateDictTypeExists(dbDictType.getId());
} }
@Test @Test
public void testCheckDictDataExists_notExists() { public void testValidateDictDataExists_notExists() {
assertServiceException(() -> dictTypeService.checkDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); assertServiceException(() -> dictTypeService.validateDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS);
} }
@Test @Test
public void testCheckDictTypeUnique_success() { public void testValidateDictTypeUnique_success() {
// 调用成功 // 调用成功
dictTypeService.checkDictTypeUnique(randomLongId(), randomString()); dictTypeService.validateDictTypeUnique(randomLongId(), randomString());
} }
@Test @Test
public void testCheckDictTypeUnique_valueDuplicateForCreate() { public void testValidateDictTypeUnique_valueDuplicateForCreate() {
// 准备参数 // 准备参数
String type = randomString(); String type = randomString();
// mock 数据 // mock 数据
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> dictTypeService.checkDictTypeUnique(null, type), assertServiceException(() -> dictTypeService.validateDictTypeUnique(null, type),
DICT_TYPE_TYPE_DUPLICATE); DICT_TYPE_TYPE_DUPLICATE);
} }
@Test @Test
public void testCheckDictTypeUnique_valueDuplicateForUpdate() { public void testValidateDictTypeUnique_valueDuplicateForUpdate() {
// 准备参数 // 准备参数
Long id = randomLongId(); Long id = randomLongId();
String type = randomString(); String type = randomString();
@ -224,30 +256,30 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> dictTypeService.checkDictTypeUnique(id, type), assertServiceException(() -> dictTypeService.validateDictTypeUnique(id, type),
DICT_TYPE_TYPE_DUPLICATE); DICT_TYPE_TYPE_DUPLICATE);
} }
@Test @Test
public void testCheckDictTypNameUnique_success() { public void testValidateDictTypNameUnique_success() {
// 调用成功 // 调用成功
dictTypeService.checkDictTypeNameUnique(randomLongId(), randomString()); dictTypeService.validateDictTypeNameUnique(randomLongId(), randomString());
} }
@Test @Test
public void testCheckDictTypeNameUnique_nameDuplicateForCreate() { public void testValidateDictTypeNameUnique_nameDuplicateForCreate() {
// 准备参数 // 准备参数
String name = randomString(); String name = randomString();
// mock 数据 // mock 数据
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(null, name), assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(null, name),
DICT_TYPE_NAME_DUPLICATE); DICT_TYPE_NAME_DUPLICATE);
} }
@Test @Test
public void testCheckDictTypeNameUnique_nameDuplicateForUpdate() { public void testValidateDictTypeNameUnique_nameDuplicateForUpdate() {
// 准备参数 // 准备参数
Long id = randomLongId(); Long id = randomLongId();
String name = randomString(); String name = randomString();
@ -255,7 +287,7 @@ public class DictTypeServiceTest extends BaseDbUnitTest {
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> dictTypeService.checkDictTypeNameUnique(id, name), assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(id, name),
DICT_TYPE_NAME_DUPLICATE); DICT_TYPE_NAME_DUPLICATE);
} }