完善 ErrorCodeServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 08:04:26 +08:00
parent 0a2fbae9d8
commit 589aaab298
6 changed files with 96 additions and 68 deletions

View File

@ -4,7 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List; import java.util.List;
@ -19,9 +18,7 @@ public interface DeptMapper extends BaseMapperX<DeptDO> {
} }
default DeptDO selectByParentIdAndName(Long parentId, String name) { default DeptDO selectByParentIdAndName(Long parentId, String name) {
return selectOne(new LambdaQueryWrapper<DeptDO>() return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
.eq(DeptDO::getParentId, parentId)
.eq(DeptDO::getName, name));
} }
default Long selectCountByParentId(Long parentId) { default Long selectCountByParentId(Long parentId) {

View File

@ -2,11 +2,10 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.Collection; import java.util.Collection;
@ -16,31 +15,32 @@ import java.util.List;
public interface PostMapper extends BaseMapperX<PostDO> { public interface PostMapper extends BaseMapperX<PostDO> {
default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) { default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) {
return selectList(new QueryWrapperX<PostDO>().inIfPresent("id", ids) return selectList(new LambdaQueryWrapperX<PostDO>()
.inIfPresent("status", statuses)); .inIfPresent(PostDO::getId, ids)
.inIfPresent(PostDO::getStatus, statuses));
} }
default PageResult<PostDO> selectPage(PostPageReqVO reqVO) { default PageResult<PostDO> selectPage(PostPageReqVO reqVO) {
return selectPage(reqVO, new QueryWrapperX<PostDO>() return selectPage(reqVO, new LambdaQueryWrapperX<PostDO>()
.likeIfPresent("code", reqVO.getCode()) .likeIfPresent(PostDO::getCode, reqVO.getCode())
.likeIfPresent("name", reqVO.getName()) .likeIfPresent(PostDO::getName, reqVO.getName())
.eqIfPresent("status", reqVO.getStatus()) .eqIfPresent(PostDO::getStatus, reqVO.getStatus())
.orderByDesc("id")); .orderByDesc(PostDO::getId));
} }
default List<PostDO> selectList(PostExportReqVO reqVO) { default List<PostDO> selectList(PostExportReqVO reqVO) {
return selectList(new QueryWrapperX<PostDO>() return selectList(new LambdaQueryWrapperX<PostDO>()
.likeIfPresent("code", reqVO.getCode()) .likeIfPresent(PostDO::getCode, reqVO.getCode())
.likeIfPresent("name", reqVO.getName()) .likeIfPresent(PostDO::getName, reqVO.getName())
.eqIfPresent("status", reqVO.getStatus())); .eqIfPresent(PostDO::getStatus, reqVO.getStatus()));
} }
default PostDO selectByName(String name) { default PostDO selectByName(String name) {
return selectOne(new QueryWrapper<PostDO>().eq("name", name)); return selectOne(PostDO::getName, name);
} }
default PostDO selectByCode(String code) { default PostDO selectByCode(String code) {
return selectOne(new QueryWrapper<PostDO>().eq("code", code)); return selectOne(PostDO::getCode, code);
} }
} }

View File

@ -28,7 +28,7 @@ public interface UserPostMapper extends BaseMapperX<UserPostDO> {
.in(UserPostDO::getPostId, postIds)); .in(UserPostDO::getPostId, postIds));
} }
default void deleteByUserId(Long userId){ default void deleteByUserId(Long userId) {
delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId)); delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
} }
} }

View File

@ -17,13 +17,11 @@ import java.util.List;
public interface DictDataMapper extends BaseMapperX<DictDataDO> { public interface DictDataMapper extends BaseMapperX<DictDataDO> {
default DictDataDO selectByDictTypeAndValue(String dictType, String value) { default DictDataDO selectByDictTypeAndValue(String dictType, String value) {
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType) return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getValue, value);
.eq(DictDataDO::getValue, value));
} }
default DictDataDO selectByDictTypeAndLabel(String dictType, String label) { default DictDataDO selectByDictTypeAndLabel(String dictType, String label) {
return selectOne(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType) return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getLabel, label);
.eq(DictDataDO::getLabel, label));
} }
default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) { default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) {

View File

@ -8,8 +8,8 @@ import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePag
import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List; import java.util.List;
@Mapper @Mapper
@ -36,11 +36,11 @@ public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> {
} }
default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) { default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) {
return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().in(ErrorCodeDO::getCode, codes)); return selectList(ErrorCodeDO::getCode, codes);
} }
default ErrorCodeDO selectByCode(Integer code) { default ErrorCodeDO selectByCode(Integer code) {
return selectOne(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getCode, code)); return selectOne(ErrorCodeDO::getCode, code);
} }
default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) { default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {

View File

@ -2,9 +2,9 @@ package cn.iocoder.yudao.module.system.service.errorcode;
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.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO;
@ -22,8 +22,9 @@ 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.framework.common.util.date.DateUtils.buildLocalDateTime; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime; 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.test.core.util.RandomUtils.*; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
@ -58,7 +59,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testUpdateErrorCode_success() { public void testUpdateErrorCode_success() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); ErrorCodeDO dbErrorCode = randomErrorCodeDO();
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
// 准备参数 // 准备参数
ErrorCodeUpdateReqVO reqVO = randomPojo(ErrorCodeUpdateReqVO.class, o -> { ErrorCodeUpdateReqVO reqVO = randomPojo(ErrorCodeUpdateReqVO.class, o -> {
@ -76,65 +77,64 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testDeleteErrorCode_success() { public void testDeleteErrorCode_success() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(); ErrorCodeDO dbErrorCode = randomErrorCodeDO();
errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据
// 准备参数 // 准备参数
Long id = dbErrorCode.getId(); Long id = dbErrorCode.getId();
// 调用 // 调用
errorCodeService.deleteErrorCode(id); errorCodeService.deleteErrorCode(id);
// 校验数据不存在了 // 校验数据不存在了
assertNull(errorCodeMapper.selectById(id)); assertNull(errorCodeMapper.selectById(id));
} }
@Test @Test
public void testGetErrorCodePage() { public void testGetErrorCodePage() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = initGetErrorCodePage(); ErrorCodeDO dbErrorCode = initGetErrorCodePage();
// 准备参数 // 准备参数
ErrorCodePageReqVO reqVO = new ErrorCodePageReqVO(); ErrorCodePageReqVO reqVO = new ErrorCodePageReqVO();
reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
reqVO.setApplicationName("tu"); reqVO.setApplicationName("tu");
reqVO.setCode(1); reqVO.setCode(1);
reqVO.setMessage("ma"); reqVO.setMessage("ma");
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2020, 11, 1), reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
buildLocalDateTime(2020, 11, 30)}));
// 调用 // 调用
PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO); PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO);
// 断言 // 断言
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbErrorCode, pageResult.getList().get(0)); assertPojoEquals(dbErrorCode, pageResult.getList().get(0));
} }
/** /**
* 初始化 getErrorCodePage 方法的测试数据 * 初始化 getErrorCodePage 方法的测试数据
*/ */
private ErrorCodeDO initGetErrorCodePage() { private ErrorCodeDO initGetErrorCodePage() {
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到 ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到
o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
o.setApplicationName("tudou"); o.setApplicationName("tudou");
o.setCode(1); o.setCode(1);
o.setMessage("yuanma"); o.setMessage("yuanma");
o.setCreateTime(buildLocalDateTime(2020, 11, 11)); o.setCreateTime(buildTime(2020, 11, 11));
}); });
errorCodeMapper.insert(dbErrorCode); errorCodeMapper.insert(dbErrorCode);
// 测试 type 不匹配 // 测试 type 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
// 测试 applicationName 不匹配 // 测试 applicationName 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan"))); errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
// 测试 code 不匹配 // 测试 code 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
// 测试 message 不匹配 // 测试 message 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai")));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildLocalDateTime(2020, 12, 12)))); errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12))));
return dbErrorCode; return dbErrorCode;
} }
@Test @Test
public void testGetErrorCodeList() { public void testGetErrorCodeList_export() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = initGetErrorCodePage(); ErrorCodeDO dbErrorCode = initGetErrorCodePage();
// 准备参数 // 准备参数
@ -143,8 +143,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
reqVO.setApplicationName("tu"); reqVO.setApplicationName("tu");
reqVO.setCode(1); reqVO.setCode(1);
reqVO.setMessage("ma"); reqVO.setMessage("ma");
reqVO.setCreateTime((new LocalDateTime[]{buildLocalDateTime(2020, 11, 1), reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
buildLocalDateTime(2020, 11, 30)}));
// 调用 // 调用
List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO); List<ErrorCodeDO> list = errorCodeService.getErrorCodeList(reqVO);
@ -158,7 +157,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
// 准备参数 // 准备参数
Integer code = randomInteger(); Integer code = randomInteger();
// mock 数据 // mock 数据
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null), assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null),
@ -171,7 +170,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
Long id = randomLongId(); Long id = randomLongId();
Integer code = randomInteger(); Integer code = randomInteger();
// mock 数据 // mock 数据
errorCodeMapper.insert(randomInfErrorCodeDO(o -> o.setCode(code))); errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code)));
// 调用校验异常 // 调用校验异常
assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id), assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id),
@ -207,7 +206,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testAutoGenerateErrorCodes_021() { public void testAutoGenerateErrorCodes_021() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())); ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()));
errorCodeMapper.insert(dbErrorCode); errorCodeMapper.insert(dbErrorCode);
// 准备参数 // 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -227,7 +226,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testAutoGenerateErrorCodes_022() { public void testAutoGenerateErrorCodes_022() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode); errorCodeMapper.insert(dbErrorCode);
// 准备参数 // 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -247,12 +246,12 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testAutoGenerateErrorCodes_023() { public void testAutoGenerateErrorCodes_023() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode); errorCodeMapper.insert(dbErrorCode);
// 准备参数 // 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName()) o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName())
.setMessage(dbErrorCode.getMessage())); .setMessage(dbErrorCode.getMessage()));
// mock 方法 // mock 方法
// 调用 // 调用
@ -268,7 +267,7 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
@Test @Test
public void testAutoGenerateErrorCodes_024() { public void testAutoGenerateErrorCodes_024() {
// mock 数据 // mock 数据
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()));
errorCodeMapper.insert(dbErrorCode); errorCodeMapper.insert(dbErrorCode);
// 准备参数 // 准备参数
ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class,
@ -282,10 +281,44 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
assertPojoEquals(generateReqDTO, errorCode); assertPojoEquals(generateReqDTO, errorCode);
} }
@Test
public void testGetErrorCode() {
// 准备参数
ErrorCodeDO errorCodeDO = randomErrorCodeDO();
errorCodeMapper.insert(errorCodeDO);
// mock 方法
Long id = errorCodeDO.getId();
// 调用
ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id);
// 断言
assertPojoEquals(errorCodeDO, dbErrorCode);
}
@Test
public void testGetErrorCodeList() {
// 准备参数
ErrorCodeDO errorCodeDO01 = randomErrorCodeDO(
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10)));
errorCodeMapper.insert(errorCodeDO01);
ErrorCodeDO errorCodeDO02 = randomErrorCodeDO(
o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12)));
errorCodeMapper.insert(errorCodeDO02);
// mock 方法
String applicationName = "yunai_server";
LocalDateTime minUpdateTime = buildTime(2022, 1, 11);
// 调用
List<ErrorCodeRespDTO> errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime);
// 断言
assertEquals(1, errorCodeList.size());
assertPojoEquals(errorCodeDO02, errorCodeList.get(0));
}
// ========== 随机对象 ========== // ========== 随机对象 ==========
@SafeVarargs @SafeVarargs
private static ErrorCodeDO randomInfErrorCodeDO(Consumer<ErrorCodeDO>... consumers) { private static ErrorCodeDO randomErrorCodeDO(Consumer<ErrorCodeDO>... consumers) {
Consumer<ErrorCodeDO> consumer = (o) -> { Consumer<ErrorCodeDO> consumer = (o) -> {
o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围 o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围
}; };