Commit 96e8df03984a23a82fd6afb92ecac9eb86747fab
1 parent
aa37c2cd
完善 NoticeServiceImpl 单元测试
Showing
4 changed files
with
46 additions
and
79 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notice/NoticeController.java
| @@ -57,8 +57,8 @@ public class NoticeController { | @@ -57,8 +57,8 @@ public class NoticeController { | ||
| 57 | @GetMapping("/page") | 57 | @GetMapping("/page") |
| 58 | @ApiOperation("获取通知公告列表") | 58 | @ApiOperation("获取通知公告列表") |
| 59 | @PreAuthorize("@ss.hasPermission('system:notice:query')") | 59 | @PreAuthorize("@ss.hasPermission('system:notice:query')") |
| 60 | - public CommonResult<PageResult<NoticeRespVO>> pageNotices(@Validated NoticePageReqVO reqVO) { | ||
| 61 | - return success(NoticeConvert.INSTANCE.convertPage(noticeService.pageNotices(reqVO))); | 60 | + public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated NoticePageReqVO reqVO) { |
| 61 | + return success(NoticeConvert.INSTANCE.convertPage(noticeService.getNoticePage(reqVO))); | ||
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | @GetMapping("/get") | 64 | @GetMapping("/get") |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeService.java
| @@ -39,7 +39,7 @@ public interface NoticeService { | @@ -39,7 +39,7 @@ public interface NoticeService { | ||
| 39 | * @param reqVO 分页条件 | 39 | * @param reqVO 分页条件 |
| 40 | * @return 部门分页列表 | 40 | * @return 部门分页列表 |
| 41 | */ | 41 | */ |
| 42 | - PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO); | 42 | + PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO); |
| 43 | 43 | ||
| 44 | /** | 44 | /** |
| 45 | * 获得岗位公告公告信息 | 45 | * 获得岗位公告公告信息 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImpl.java
| 1 | package cn.iocoder.yudao.module.system.service.notice; | 1 | package cn.iocoder.yudao.module.system.service.notice; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; | 4 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; | 5 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert; | 7 | import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert; |
| 9 | -import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | ||
| 10 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | +import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | ||
| 11 | import com.google.common.annotations.VisibleForTesting; | 10 | import com.google.common.annotations.VisibleForTesting; |
| 12 | import org.springframework.stereotype.Service; | 11 | import org.springframework.stereotype.Service; |
| 13 | 12 | ||
| 14 | import javax.annotation.Resource; | 13 | import javax.annotation.Resource; |
| 15 | 14 | ||
| 15 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 16 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | 16 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; |
| 17 | 17 | ||
| 18 | /** | 18 | /** |
| @@ -36,7 +36,7 @@ public class NoticeServiceImpl implements NoticeService { | @@ -36,7 +36,7 @@ public class NoticeServiceImpl implements NoticeService { | ||
| 36 | @Override | 36 | @Override |
| 37 | public void updateNotice(NoticeUpdateReqVO reqVO) { | 37 | public void updateNotice(NoticeUpdateReqVO reqVO) { |
| 38 | // 校验是否存在 | 38 | // 校验是否存在 |
| 39 | - this.checkNoticeExists(reqVO.getId()); | 39 | + validateNoticeExists(reqVO.getId()); |
| 40 | // 更新通知公告 | 40 | // 更新通知公告 |
| 41 | NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO); | 41 | NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO); |
| 42 | noticeMapper.updateById(updateObj); | 42 | noticeMapper.updateById(updateObj); |
| @@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService { | @@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService { | ||
| 45 | @Override | 45 | @Override |
| 46 | public void deleteNotice(Long id) { | 46 | public void deleteNotice(Long id) { |
| 47 | // 校验是否存在 | 47 | // 校验是否存在 |
| 48 | - this.checkNoticeExists(id); | 48 | + validateNoticeExists(id); |
| 49 | // 删除通知公告 | 49 | // 删除通知公告 |
| 50 | noticeMapper.deleteById(id); | 50 | noticeMapper.deleteById(id); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | @Override | 53 | @Override |
| 54 | - public PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO) { | 54 | + public PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO) { |
| 55 | return noticeMapper.selectPage(reqVO); | 55 | return noticeMapper.selectPage(reqVO); |
| 56 | } | 56 | } |
| 57 | 57 | ||
| @@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService { | @@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService { | ||
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | @VisibleForTesting | 63 | @VisibleForTesting |
| 64 | - public void checkNoticeExists(Long id) { | 64 | + public void validateNoticeExists(Long id) { |
| 65 | if (id == null) { | 65 | if (id == null) { |
| 66 | return; | 66 | return; |
| 67 | } | 67 | } |
| 68 | NoticeDO notice = noticeMapper.selectById(id); | 68 | NoticeDO notice = noticeMapper.selectById(id); |
| 69 | if (notice == null) { | 69 | if (notice == null) { |
| 70 | - throw ServiceExceptionUtil.exception(NOTICE_NOT_FOUND); | 70 | + throw exception(NOTICE_NOT_FOUND); |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | 73 |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImplTest.java
| @@ -2,75 +2,67 @@ package cn.iocoder.yudao.module.system.service.notice; | @@ -2,75 +2,67 @@ package cn.iocoder.yudao.module.system.service.notice; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||
| 5 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; | 8 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | 10 | import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; |
| 10 | -import cn.iocoder.yudao.module.system.enums.notice.NoticeTypeEnum; | ||
| 11 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||
| 12 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||
| 13 | import org.junit.jupiter.api.Test; | 11 | import org.junit.jupiter.api.Test; |
| 14 | import org.springframework.context.annotation.Import; | 12 | import org.springframework.context.annotation.Import; |
| 15 | 13 | ||
| 16 | import javax.annotation.Resource; | 14 | import javax.annotation.Resource; |
| 17 | 15 | ||
| 18 | -import java.util.function.Consumer; | ||
| 19 | - | ||
| 20 | -import static cn.hutool.core.util.RandomUtil.randomEle; | ||
| 21 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | 16 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 22 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | 17 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 23 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | 18 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 24 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | 19 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 25 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 20 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 21 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; | ||
| 26 | import static org.junit.jupiter.api.Assertions.*; | 22 | import static org.junit.jupiter.api.Assertions.*; |
| 27 | 23 | ||
| 28 | @Import(NoticeServiceImpl.class) | 24 | @Import(NoticeServiceImpl.class) |
| 29 | class NoticeServiceImplTest extends BaseDbUnitTest { | 25 | class NoticeServiceImplTest extends BaseDbUnitTest { |
| 30 | 26 | ||
| 31 | @Resource | 27 | @Resource |
| 32 | - private NoticeServiceImpl sysNoticeService; | 28 | + private NoticeServiceImpl noticeService; |
| 33 | 29 | ||
| 34 | @Resource | 30 | @Resource |
| 35 | - private NoticeMapper sysNoticeMapper; | 31 | + private NoticeMapper noticeMapper; |
| 36 | 32 | ||
| 37 | @Test | 33 | @Test |
| 38 | - public void testPageNotices_success() { | 34 | + public void testGetNoticePage_success() { |
| 39 | // 插入前置数据 | 35 | // 插入前置数据 |
| 40 | NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { | 36 | NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { |
| 41 | o.setTitle("尼古拉斯赵四来啦!"); | 37 | o.setTitle("尼古拉斯赵四来啦!"); |
| 42 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | 38 | o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 43 | - o.setType(randomEle(NoticeTypeEnum.values()).getType()); | ||
| 44 | }); | 39 | }); |
| 45 | - sysNoticeMapper.insert(dbNotice); | ||
| 46 | - | 40 | + noticeMapper.insert(dbNotice); |
| 47 | // 测试 title 不匹配 | 41 | // 测试 title 不匹配 |
| 48 | - sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); | 42 | + noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); |
| 49 | // 测试 status 不匹配 | 43 | // 测试 status 不匹配 |
| 50 | - sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - // 查询 | 44 | + noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
| 45 | + // 准备参数 | ||
| 54 | NoticePageReqVO reqVO = new NoticePageReqVO(); | 46 | NoticePageReqVO reqVO = new NoticePageReqVO(); |
| 55 | reqVO.setTitle("尼古拉斯赵四来啦!"); | 47 | reqVO.setTitle("尼古拉斯赵四来啦!"); |
| 56 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | 48 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 57 | - PageResult<NoticeDO> pageResult = sysNoticeService.pageNotices(reqVO); | ||
| 58 | 49 | ||
| 50 | + // 调用 | ||
| 51 | + PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO); | ||
| 59 | // 验证查询结果经过筛选 | 52 | // 验证查询结果经过筛选 |
| 60 | assertEquals(1, pageResult.getTotal()); | 53 | assertEquals(1, pageResult.getTotal()); |
| 61 | assertEquals(1, pageResult.getList().size()); | 54 | assertEquals(1, pageResult.getList().size()); |
| 62 | assertPojoEquals(dbNotice, pageResult.getList().get(0)); | 55 | assertPojoEquals(dbNotice, pageResult.getList().get(0)); |
| 63 | - | ||
| 64 | } | 56 | } |
| 65 | 57 | ||
| 66 | @Test | 58 | @Test |
| 67 | public void testGetNotice_success() { | 59 | public void testGetNotice_success() { |
| 68 | // 插入前置数据 | 60 | // 插入前置数据 |
| 69 | - NoticeDO dbNotice = randomNoticeDO(); | ||
| 70 | - sysNoticeMapper.insert(dbNotice); | 61 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); |
| 62 | + noticeMapper.insert(dbNotice); | ||
| 71 | 63 | ||
| 72 | // 查询 | 64 | // 查询 |
| 73 | - NoticeDO notice = sysNoticeService.getNotice(dbNotice.getId()); | 65 | + NoticeDO notice = noticeService.getNotice(dbNotice.getId()); |
| 74 | 66 | ||
| 75 | // 验证插入与读取对象是否一致 | 67 | // 验证插入与读取对象是否一致 |
| 76 | assertNotNull(notice); | 68 | assertNotNull(notice); |
| @@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest { | @@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest { | ||
| 80 | @Test | 72 | @Test |
| 81 | public void testCreateNotice_success() { | 73 | public void testCreateNotice_success() { |
| 82 | // 准备参数 | 74 | // 准备参数 |
| 83 | - NoticeCreateReqVO reqVO = randomNoticeCreateReqVO(); | ||
| 84 | - | ||
| 85 | - // 校验插入是否成功 | ||
| 86 | - Long noticeId = sysNoticeService.createNotice(reqVO); | ||
| 87 | - assertNotNull(noticeId); | 75 | + NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class); |
| 88 | 76 | ||
| 77 | + // 调用 | ||
| 78 | + Long noticeId = noticeService.createNotice(reqVO); | ||
| 89 | // 校验插入属性是否正确 | 79 | // 校验插入属性是否正确 |
| 90 | - NoticeDO notice = sysNoticeMapper.selectById(noticeId); | 80 | + assertNotNull(noticeId); |
| 81 | + NoticeDO notice = noticeMapper.selectById(noticeId); | ||
| 91 | assertPojoEquals(reqVO, notice); | 82 | assertPojoEquals(reqVO, notice); |
| 92 | } | 83 | } |
| 93 | 84 | ||
| 94 | @Test | 85 | @Test |
| 95 | public void testUpdateNotice_success() { | 86 | public void testUpdateNotice_success() { |
| 96 | // 插入前置数据 | 87 | // 插入前置数据 |
| 97 | - NoticeDO dbNoticeDO = randomNoticeDO(); | ||
| 98 | - sysNoticeMapper.insert(dbNoticeDO); | 88 | + NoticeDO dbNoticeDO = randomPojo(NoticeDO.class); |
| 89 | + noticeMapper.insert(dbNoticeDO); | ||
| 99 | 90 | ||
| 100 | // 准备更新参数 | 91 | // 准备更新参数 |
| 101 | - NoticeUpdateReqVO reqVO = randomNoticeUpdateReqVO(o -> o.setId(dbNoticeDO.getId())); | 92 | + NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, o -> o.setId(dbNoticeDO.getId())); |
| 102 | 93 | ||
| 103 | // 更新 | 94 | // 更新 |
| 104 | - sysNoticeService.updateNotice(reqVO); | ||
| 105 | - | 95 | + noticeService.updateNotice(reqVO); |
| 106 | // 检验是否更新成功 | 96 | // 检验是否更新成功 |
| 107 | - NoticeDO notice = sysNoticeMapper.selectById(reqVO.getId()); | 97 | + NoticeDO notice = noticeMapper.selectById(reqVO.getId()); |
| 108 | assertPojoEquals(reqVO, notice); | 98 | assertPojoEquals(reqVO, notice); |
| 109 | } | 99 | } |
| 110 | 100 | ||
| 111 | @Test | 101 | @Test |
| 112 | public void testDeleteNotice_success() { | 102 | public void testDeleteNotice_success() { |
| 113 | // 插入前置数据 | 103 | // 插入前置数据 |
| 114 | - NoticeDO dbNotice = randomNoticeDO(); | ||
| 115 | - sysNoticeMapper.insert(dbNotice); | 104 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); |
| 105 | + noticeMapper.insert(dbNotice); | ||
| 116 | 106 | ||
| 117 | // 删除 | 107 | // 删除 |
| 118 | - sysNoticeService.deleteNotice(dbNotice.getId()); | 108 | + noticeService.deleteNotice(dbNotice.getId()); |
| 119 | 109 | ||
| 120 | // 检查是否删除成功 | 110 | // 检查是否删除成功 |
| 121 | - assertNull(sysNoticeMapper.selectById(dbNotice.getId())); | 111 | + assertNull(noticeMapper.selectById(dbNotice.getId())); |
| 122 | } | 112 | } |
| 123 | 113 | ||
| 124 | @Test | 114 | @Test |
| 125 | - public void checkNoticeExists_success() { | 115 | + public void testValidateNoticeExists_success() { |
| 126 | // 插入前置数据 | 116 | // 插入前置数据 |
| 127 | - NoticeDO dbNotice = randomNoticeDO(); | ||
| 128 | - sysNoticeMapper.insert(dbNotice); | 117 | + NoticeDO dbNotice = randomPojo(NoticeDO.class); |
| 118 | + noticeMapper.insert(dbNotice); | ||
| 129 | 119 | ||
| 130 | // 成功调用 | 120 | // 成功调用 |
| 131 | - sysNoticeService.checkNoticeExists(dbNotice.getId()); | 121 | + noticeService.validateNoticeExists(dbNotice.getId()); |
| 132 | } | 122 | } |
| 133 | 123 | ||
| 134 | @Test | 124 | @Test |
| 135 | - public void checkNoticeExists_noExists() { | ||
| 136 | - assertServiceException(() -> sysNoticeService.checkNoticeExists(randomLongId()), NOTICE_NOT_FOUND); | 125 | + public void testValidateNoticeExists_noExists() { |
| 126 | + assertServiceException(() -> | ||
| 127 | + noticeService.validateNoticeExists(randomLongId()), NOTICE_NOT_FOUND); | ||
| 137 | } | 128 | } |
| 138 | 129 | ||
| 139 | - @SafeVarargs | ||
| 140 | - private static NoticeDO randomNoticeDO(Consumer<NoticeDO>... consumers) { | ||
| 141 | - NoticeDO notice = randomPojo(NoticeDO.class, consumers); | ||
| 142 | - notice.setType(randomEle(NoticeTypeEnum.values()).getType()); | ||
| 143 | - notice.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 144 | - return notice; | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | - @SafeVarargs | ||
| 148 | - private static NoticeUpdateReqVO randomNoticeUpdateReqVO(Consumer<NoticeUpdateReqVO>... consumers) { | ||
| 149 | - NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, consumers); | ||
| 150 | - reqVO.setType(randomEle(NoticeTypeEnum.values()).getType()); | ||
| 151 | - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 152 | - return reqVO; | ||
| 153 | - } | ||
| 154 | - | ||
| 155 | - private static NoticeCreateReqVO randomNoticeCreateReqVO() { | ||
| 156 | - NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class); | ||
| 157 | - reqVO.setType(randomEle(NoticeTypeEnum.values()).getType()); | ||
| 158 | - reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 159 | - return reqVO; | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - | ||
| 163 | } | 130 | } |