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 | 57 | @GetMapping("/page") |
| 58 | 58 | @ApiOperation("获取通知公告列表") |
| 59 | 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 | 64 | @GetMapping("/get") | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeService.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.notice; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 4 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | 5 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | 7 | import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert; |
| 9 | -import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | |
| 10 | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | +import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; | |
| 11 | 10 | import com.google.common.annotations.VisibleForTesting; |
| 12 | 11 | import org.springframework.stereotype.Service; |
| 13 | 12 | |
| 14 | 13 | import javax.annotation.Resource; |
| 15 | 14 | |
| 15 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 16 | 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 | 36 | @Override |
| 37 | 37 | public void updateNotice(NoticeUpdateReqVO reqVO) { |
| 38 | 38 | // 校验是否存在 |
| 39 | - this.checkNoticeExists(reqVO.getId()); | |
| 39 | + validateNoticeExists(reqVO.getId()); | |
| 40 | 40 | // 更新通知公告 |
| 41 | 41 | NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO); |
| 42 | 42 | noticeMapper.updateById(updateObj); |
| ... | ... | @@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService { |
| 45 | 45 | @Override |
| 46 | 46 | public void deleteNotice(Long id) { |
| 47 | 47 | // 校验是否存在 |
| 48 | - this.checkNoticeExists(id); | |
| 48 | + validateNoticeExists(id); | |
| 49 | 49 | // 删除通知公告 |
| 50 | 50 | noticeMapper.deleteById(id); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | @Override |
| 54 | - public PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO) { | |
| 54 | + public PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO) { | |
| 55 | 55 | return noticeMapper.selectPage(reqVO); |
| 56 | 56 | } |
| 57 | 57 | |
| ... | ... | @@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService { |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | @VisibleForTesting |
| 64 | - public void checkNoticeExists(Long id) { | |
| 64 | + public void validateNoticeExists(Long id) { | |
| 65 | 65 | if (id == null) { |
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | NoticeDO notice = noticeMapper.selectById(id); |
| 69 | 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 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 5 | 6 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; |
| 9 | 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 | 11 | import org.junit.jupiter.api.Test; |
| 14 | 12 | import org.springframework.context.annotation.Import; |
| 15 | 13 | |
| 16 | 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 | 17 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 23 | 18 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 24 | 19 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 25 | 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 | 22 | import static org.junit.jupiter.api.Assertions.*; |
| 27 | 23 | |
| 28 | 24 | @Import(NoticeServiceImpl.class) |
| 29 | 25 | class NoticeServiceImplTest extends BaseDbUnitTest { |
| 30 | 26 | |
| 31 | 27 | @Resource |
| 32 | - private NoticeServiceImpl sysNoticeService; | |
| 28 | + private NoticeServiceImpl noticeService; | |
| 33 | 29 | |
| 34 | 30 | @Resource |
| 35 | - private NoticeMapper sysNoticeMapper; | |
| 31 | + private NoticeMapper noticeMapper; | |
| 36 | 32 | |
| 37 | 33 | @Test |
| 38 | - public void testPageNotices_success() { | |
| 34 | + public void testGetNoticePage_success() { | |
| 39 | 35 | // 插入前置数据 |
| 40 | 36 | NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { |
| 41 | 37 | o.setTitle("尼古拉斯赵四来啦!"); |
| 42 | 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 | 41 | // 测试 title 不匹配 |
| 48 | - sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); | |
| 42 | + noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); | |
| 49 | 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 | 46 | NoticePageReqVO reqVO = new NoticePageReqVO(); |
| 55 | 47 | reqVO.setTitle("尼古拉斯赵四来啦!"); |
| 56 | 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 | 53 | assertEquals(1, pageResult.getTotal()); |
| 61 | 54 | assertEquals(1, pageResult.getList().size()); |
| 62 | 55 | assertPojoEquals(dbNotice, pageResult.getList().get(0)); |
| 63 | - | |
| 64 | 56 | } |
| 65 | 57 | |
| 66 | 58 | @Test |
| 67 | 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 | 68 | assertNotNull(notice); |
| ... | ... | @@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest { |
| 80 | 72 | @Test |
| 81 | 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 | 82 | assertPojoEquals(reqVO, notice); |
| 92 | 83 | } |
| 93 | 84 | |
| 94 | 85 | @Test |
| 95 | 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 | 98 | assertPojoEquals(reqVO, notice); |
| 109 | 99 | } |
| 110 | 100 | |
| 111 | 101 | @Test |
| 112 | 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 | 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 | 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 | } | ... | ... |