Commit 3b6359caa04dd7a9483306d0539fc055dadf829b
1 parent
cee1aa3e
job 单元测试 Demo
Showing
5 changed files
with
102 additions
and
0 deletions
src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java
| @@ -34,4 +34,7 @@ public class InfJobBaseVO { | @@ -34,4 +34,7 @@ public class InfJobBaseVO { | ||
| 34 | @ApiModelProperty(value = "监控超时时间", example = "1000") | 34 | @ApiModelProperty(value = "监控超时时间", example = "1000") |
| 35 | private Integer monitorTimeout; | 35 | private Integer monitorTimeout; |
| 36 | 36 | ||
| 37 | + public void setCronExpression(String cronExpression) { | ||
| 38 | + this.cronExpression = cronExpression; | ||
| 39 | + } | ||
| 37 | } | 40 | } |
src/main/java/cn/iocoder/dashboard/modules/infra/service/job/impl/InfJobServiceImpl.java
| @@ -13,6 +13,7 @@ import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; | @@ -13,6 +13,7 @@ import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; | ||
| 13 | import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; | 13 | import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; |
| 14 | import cn.iocoder.dashboard.modules.infra.service.job.InfJobService; | 14 | import cn.iocoder.dashboard.modules.infra.service.job.InfJobService; |
| 15 | import org.quartz.SchedulerException; | 15 | import org.quartz.SchedulerException; |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 16 | import org.springframework.stereotype.Service; | 17 | import org.springframework.stereotype.Service; |
| 17 | import org.springframework.transaction.annotation.Transactional; | 18 | import org.springframework.transaction.annotation.Transactional; |
| 18 | import org.springframework.validation.annotation.Validated; | 19 | import org.springframework.validation.annotation.Validated; |
| @@ -37,6 +38,7 @@ public class InfJobServiceImpl implements InfJobService { | @@ -37,6 +38,7 @@ public class InfJobServiceImpl implements InfJobService { | ||
| 37 | @Resource | 38 | @Resource |
| 38 | private InfJobMapper jobMapper; | 39 | private InfJobMapper jobMapper; |
| 39 | 40 | ||
| 41 | + @MockBean | ||
| 40 | @Resource | 42 | @Resource |
| 41 | private SchedulerManager schedulerManager; | 43 | private SchedulerManager schedulerManager; |
| 42 | 44 |
src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.infra.service.job; | ||
| 2 | + | ||
| 3 | +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||
| 4 | +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| 7 | +import static org.mockito.Mockito.times; | ||
| 8 | +import static org.mockito.Mockito.verify; | ||
| 9 | + | ||
| 10 | +import javax.annotation.Resource; | ||
| 11 | + | ||
| 12 | +import org.junit.jupiter.api.Test; | ||
| 13 | +import org.quartz.SchedulerException; | ||
| 14 | +import org.springframework.context.annotation.Import; | ||
| 15 | +import cn.iocoder.dashboard.BaseDbUnitTest; | ||
| 16 | +import cn.iocoder.dashboard.framework.quartz.core.scheduler.SchedulerManager; | ||
| 17 | +import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.InfJobCreateReqVO; | ||
| 18 | +import cn.iocoder.dashboard.modules.infra.convert.job.InfJobConvert; | ||
| 19 | +import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO; | ||
| 20 | +import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; | ||
| 21 | +import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; | ||
| 22 | +import cn.iocoder.dashboard.modules.infra.service.job.impl.InfJobServiceImpl; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * {@link InfJobServiceImpl} 的单元测试 | ||
| 26 | + * | ||
| 27 | + * @author neilz | ||
| 28 | + */ | ||
| 29 | +@Import(InfJobServiceImpl.class) | ||
| 30 | +public class InfJobServiceTest extends BaseDbUnitTest { | ||
| 31 | + @Resource | ||
| 32 | + private InfJobServiceImpl jobService; | ||
| 33 | + | ||
| 34 | + @Resource | ||
| 35 | + private InfJobMapper jobMapper; | ||
| 36 | + @Resource | ||
| 37 | + private SchedulerManager schedulerManager; | ||
| 38 | + | ||
| 39 | + @Test | ||
| 40 | + public void testCreateJob_success() throws SchedulerException { | ||
| 41 | + // 准备参数 | ||
| 42 | + InfJobCreateReqVO reqVO = randomPojo(InfJobCreateReqVO.class); | ||
| 43 | + reqVO.setCronExpression("0 0/1 * * * ? *"); | ||
| 44 | + | ||
| 45 | + // 调用 | ||
| 46 | + Long jobId = jobService.createJob(reqVO); | ||
| 47 | + | ||
| 48 | + // 断言 | ||
| 49 | + assertNotNull(jobId); | ||
| 50 | + | ||
| 51 | + // 校验记录的属性是否正确 | ||
| 52 | + InfJobDO job = jobMapper.selectById(jobId); | ||
| 53 | + assertPojoEquals(reqVO, job); | ||
| 54 | + assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus()); | ||
| 55 | + | ||
| 56 | + // 校验调用 | ||
| 57 | + verify(jobMapper, times(1)).selectByHandlerName(reqVO.getHandlerName()); | ||
| 58 | + | ||
| 59 | + InfJobDO insertJob = InfJobConvert.INSTANCE.convert(reqVO); | ||
| 60 | + insertJob.setStatus(InfJobStatusEnum.INIT.getStatus()); | ||
| 61 | + fillJobMonitorTimeoutEmpty(insertJob); | ||
| 62 | + verify(jobMapper, times(1)).insert(insertJob); | ||
| 63 | + | ||
| 64 | + verify(schedulerManager, times(1)).addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), | ||
| 65 | + job.getRetryCount(), job.getRetryInterval()); | ||
| 66 | + | ||
| 67 | + InfJobDO updateObj = InfJobDO.builder().id(insertJob.getId()).status(InfJobStatusEnum.NORMAL.getStatus()).build(); | ||
| 68 | + verify(jobMapper, times(1)).updateById(updateObj); | ||
| 69 | + | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + private static void fillJobMonitorTimeoutEmpty(InfJobDO job) { | ||
| 73 | + if (job.getMonitorTimeout() == null) { | ||
| 74 | + job.setMonitorTimeout(0); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | +} |
src/test/resources/sql/clean.sql
src/test/resources/sql/create_tables.sql
| @@ -29,6 +29,24 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( | @@ -29,6 +29,24 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( | ||
| 29 | PRIMARY KEY ("id") | 29 | PRIMARY KEY ("id") |
| 30 | ) COMMENT '文件表'; | 30 | ) COMMENT '文件表'; |
| 31 | 31 | ||
| 32 | +CREATE TABLE "inf_job" ( | ||
| 33 | + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '任务编号', | ||
| 34 | + "name" varchar(32) NOT NULL COMMENT '任务名称', | ||
| 35 | + "status" tinyint(4) NOT NULL COMMENT '任务状态', | ||
| 36 | + "handler_name" varchar(64) NOT NULL COMMENT '处理器的名字', | ||
| 37 | + "handler_param" varchar(255) DEFAULT NULL COMMENT '处理器的参数', | ||
| 38 | + "cron_expression" varchar(32) NOT NULL COMMENT 'CRON 表达式', | ||
| 39 | + "retry_count" int(11) NOT NULL DEFAULT '0' COMMENT '重试次数', | ||
| 40 | + "retry_interval" int(11) NOT NULL DEFAULT '0' COMMENT '重试间隔', | ||
| 41 | + "monitor_timeout" int(11) NOT NULL DEFAULT '0' COMMENT '监控超时时间', | ||
| 42 | + "creator" varchar(64) DEFAULT '' COMMENT '创建者', | ||
| 43 | + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||
| 44 | + "updater" varchar(64) DEFAULT '' COMMENT '更新者', | ||
| 45 | + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | ||
| 46 | + "deleted" bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', | ||
| 47 | + PRIMARY KEY ("id") | ||
| 48 | +) COMMENT='定时任务表'; | ||
| 49 | + | ||
| 32 | -- sys 开头的 DB | 50 | -- sys 开头的 DB |
| 33 | 51 | ||
| 34 | CREATE TABLE IF NOT EXISTS "sys_dept" ( | 52 | CREATE TABLE IF NOT EXISTS "sys_dept" ( |