完善 NoticeServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 21:04:30 +08:00
parent 608f177200
commit dfffbe3823
4 changed files with 46 additions and 79 deletions

View File

@ -57,8 +57,8 @@ public class NoticeController {
@GetMapping("/page") @GetMapping("/page")
@ApiOperation("获取通知公告列表") @ApiOperation("获取通知公告列表")
@PreAuthorize("@ss.hasPermission('system:notice:query')") @PreAuthorize("@ss.hasPermission('system:notice:query')")
public CommonResult<PageResult<NoticeRespVO>> pageNotices(@Validated NoticePageReqVO reqVO) { public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated NoticePageReqVO reqVO) {
return success(NoticeConvert.INSTANCE.convertPage(noticeService.pageNotices(reqVO))); return success(NoticeConvert.INSTANCE.convertPage(noticeService.getNoticePage(reqVO)));
} }
@GetMapping("/get") @GetMapping("/get")

View File

@ -39,7 +39,7 @@ public interface NoticeService {
* @param reqVO 分页条件 * @param reqVO 分页条件
* @return 部门分页列表 * @return 部门分页列表
*/ */
PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO); PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO);
/** /**
* 获得岗位公告公告信息 * 获得岗位公告公告信息

View File

@ -1,18 +1,18 @@
package cn.iocoder.yudao.module.system.service.notice; package cn.iocoder.yudao.module.system.service.notice;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert; import cn.iocoder.yudao.module.system.convert.notice.NoticeConvert;
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO;
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
/** /**
@ -36,7 +36,7 @@ public class NoticeServiceImpl implements NoticeService {
@Override @Override
public void updateNotice(NoticeUpdateReqVO reqVO) { public void updateNotice(NoticeUpdateReqVO reqVO) {
// 校验是否存在 // 校验是否存在
this.checkNoticeExists(reqVO.getId()); validateNoticeExists(reqVO.getId());
// 更新通知公告 // 更新通知公告
NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO); NoticeDO updateObj = NoticeConvert.INSTANCE.convert(reqVO);
noticeMapper.updateById(updateObj); noticeMapper.updateById(updateObj);
@ -45,13 +45,13 @@ public class NoticeServiceImpl implements NoticeService {
@Override @Override
public void deleteNotice(Long id) { public void deleteNotice(Long id) {
// 校验是否存在 // 校验是否存在
this.checkNoticeExists(id); validateNoticeExists(id);
// 删除通知公告 // 删除通知公告
noticeMapper.deleteById(id); noticeMapper.deleteById(id);
} }
@Override @Override
public PageResult<NoticeDO> pageNotices(NoticePageReqVO reqVO) { public PageResult<NoticeDO> getNoticePage(NoticePageReqVO reqVO) {
return noticeMapper.selectPage(reqVO); return noticeMapper.selectPage(reqVO);
} }
@ -61,13 +61,13 @@ public class NoticeServiceImpl implements NoticeService {
} }
@VisibleForTesting @VisibleForTesting
public void checkNoticeExists(Long id) { public void validateNoticeExists(Long id) {
if (id == null) { if (id == null) {
return; return;
} }
NoticeDO notice = noticeMapper.selectById(id); NoticeDO notice = noticeMapper.selectById(id);
if (notice == null) { if (notice == null) {
throw ServiceExceptionUtil.exception(NOTICE_NOT_FOUND); throw exception(NOTICE_NOT_FOUND);
} }
} }

View File

@ -2,75 +2,67 @@ package cn.iocoder.yudao.module.system.service.notice;
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.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticeUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO; import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO;
import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper; import cn.iocoder.yudao.module.system.dal.mysql.notice.NoticeMapper;
import cn.iocoder.yudao.module.system.enums.notice.NoticeTypeEnum;
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.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.function.Consumer; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
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.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@Import(NoticeServiceImpl.class) @Import(NoticeServiceImpl.class)
class NoticeServiceImplTest extends BaseDbUnitTest { class NoticeServiceImplTest extends BaseDbUnitTest {
@Resource @Resource
private NoticeServiceImpl sysNoticeService; private NoticeServiceImpl noticeService;
@Resource @Resource
private NoticeMapper sysNoticeMapper; private NoticeMapper noticeMapper;
@Test @Test
public void testPageNotices_success() { public void testGetNoticePage_success() {
// 插入前置数据 // 插入前置数据
NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> {
o.setTitle("尼古拉斯赵四来啦!"); o.setTitle("尼古拉斯赵四来啦!");
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setType(randomEle(NoticeTypeEnum.values()).getType());
}); });
sysNoticeMapper.insert(dbNotice); noticeMapper.insert(dbNotice);
// 测试 title 不匹配 // 测试 title 不匹配
sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!")));
// 测试 status 不匹配 // 测试 status 不匹配
sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
// 查询
NoticePageReqVO reqVO = new NoticePageReqVO(); NoticePageReqVO reqVO = new NoticePageReqVO();
reqVO.setTitle("尼古拉斯赵四来啦!"); reqVO.setTitle("尼古拉斯赵四来啦!");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
PageResult<NoticeDO> pageResult = sysNoticeService.pageNotices(reqVO);
// 调用
PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO);
// 验证查询结果经过筛选 // 验证查询结果经过筛选
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbNotice, pageResult.getList().get(0)); assertPojoEquals(dbNotice, pageResult.getList().get(0));
} }
@Test @Test
public void testGetNotice_success() { public void testGetNotice_success() {
// 插入前置数据 // 插入前置数据
NoticeDO dbNotice = randomNoticeDO(); NoticeDO dbNotice = randomPojo(NoticeDO.class);
sysNoticeMapper.insert(dbNotice); noticeMapper.insert(dbNotice);
// 查询 // 查询
NoticeDO notice = sysNoticeService.getNotice(dbNotice.getId()); NoticeDO notice = noticeService.getNotice(dbNotice.getId());
// 验证插入与读取对象是否一致 // 验证插入与读取对象是否一致
assertNotNull(notice); assertNotNull(notice);
@ -80,84 +72,59 @@ class NoticeServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testCreateNotice_success() { public void testCreateNotice_success() {
// 准备参数 // 准备参数
NoticeCreateReqVO reqVO = randomNoticeCreateReqVO(); NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class);
// 校验插入是否成功
Long noticeId = sysNoticeService.createNotice(reqVO);
assertNotNull(noticeId);
// 调用
Long noticeId = noticeService.createNotice(reqVO);
// 校验插入属性是否正确 // 校验插入属性是否正确
NoticeDO notice = sysNoticeMapper.selectById(noticeId); assertNotNull(noticeId);
NoticeDO notice = noticeMapper.selectById(noticeId);
assertPojoEquals(reqVO, notice); assertPojoEquals(reqVO, notice);
} }
@Test @Test
public void testUpdateNotice_success() { public void testUpdateNotice_success() {
// 插入前置数据 // 插入前置数据
NoticeDO dbNoticeDO = randomNoticeDO(); NoticeDO dbNoticeDO = randomPojo(NoticeDO.class);
sysNoticeMapper.insert(dbNoticeDO); noticeMapper.insert(dbNoticeDO);
// 准备更新参数 // 准备更新参数
NoticeUpdateReqVO reqVO = randomNoticeUpdateReqVO(o -> o.setId(dbNoticeDO.getId())); NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, o -> o.setId(dbNoticeDO.getId()));
// 更新 // 更新
sysNoticeService.updateNotice(reqVO); noticeService.updateNotice(reqVO);
// 检验是否更新成功 // 检验是否更新成功
NoticeDO notice = sysNoticeMapper.selectById(reqVO.getId()); NoticeDO notice = noticeMapper.selectById(reqVO.getId());
assertPojoEquals(reqVO, notice); assertPojoEquals(reqVO, notice);
} }
@Test @Test
public void testDeleteNotice_success() { public void testDeleteNotice_success() {
// 插入前置数据 // 插入前置数据
NoticeDO dbNotice = randomNoticeDO(); NoticeDO dbNotice = randomPojo(NoticeDO.class);
sysNoticeMapper.insert(dbNotice); noticeMapper.insert(dbNotice);
// 删除 // 删除
sysNoticeService.deleteNotice(dbNotice.getId()); noticeService.deleteNotice(dbNotice.getId());
// 检查是否删除成功 // 检查是否删除成功
assertNull(sysNoticeMapper.selectById(dbNotice.getId())); assertNull(noticeMapper.selectById(dbNotice.getId()));
} }
@Test @Test
public void checkNoticeExists_success() { public void testValidateNoticeExists_success() {
// 插入前置数据 // 插入前置数据
NoticeDO dbNotice = randomNoticeDO(); NoticeDO dbNotice = randomPojo(NoticeDO.class);
sysNoticeMapper.insert(dbNotice); noticeMapper.insert(dbNotice);
// 成功调用 // 成功调用
sysNoticeService.checkNoticeExists(dbNotice.getId()); noticeService.validateNoticeExists(dbNotice.getId());
} }
@Test @Test
public void checkNoticeExists_noExists() { public void testValidateNoticeExists_noExists() {
assertServiceException(() -> sysNoticeService.checkNoticeExists(randomLongId()), NOTICE_NOT_FOUND); assertServiceException(() ->
noticeService.validateNoticeExists(randomLongId()), NOTICE_NOT_FOUND);
} }
@SafeVarargs
private static NoticeDO randomNoticeDO(Consumer<NoticeDO>... consumers) {
NoticeDO notice = randomPojo(NoticeDO.class, consumers);
notice.setType(randomEle(NoticeTypeEnum.values()).getType());
notice.setStatus(CommonStatusEnum.ENABLE.getStatus());
return notice;
}
@SafeVarargs
private static NoticeUpdateReqVO randomNoticeUpdateReqVO(Consumer<NoticeUpdateReqVO>... consumers) {
NoticeUpdateReqVO reqVO = randomPojo(NoticeUpdateReqVO.class, consumers);
reqVO.setType(randomEle(NoticeTypeEnum.values()).getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
return reqVO;
}
private static NoticeCreateReqVO randomNoticeCreateReqVO() {
NoticeCreateReqVO reqVO = randomPojo(NoticeCreateReqVO.class);
reqVO.setType(randomEle(NoticeTypeEnum.values()).getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
return reqVO;
}
} }