mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	job 单元测试 Demo
This commit is contained in:
		| @@ -34,4 +34,7 @@ public class InfJobBaseVO { | ||||
|     @ApiModelProperty(value = "监控超时时间", example = "1000") | ||||
|     private Integer monitorTimeout; | ||||
|  | ||||
|     public void setCronExpression(String cronExpression) { | ||||
|         this.cronExpression = cronExpression; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -13,6 +13,7 @@ import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; | ||||
| import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; | ||||
| import cn.iocoder.dashboard.modules.infra.service.job.InfJobService; | ||||
| import org.quartz.SchedulerException; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| @@ -37,6 +38,7 @@ public class InfJobServiceImpl implements InfJobService { | ||||
|     @Resource | ||||
|     private InfJobMapper jobMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     @Resource | ||||
|     private SchedulerManager schedulerManager; | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,78 @@ | ||||
| package cn.iocoder.dashboard.modules.infra.service.job; | ||||
|  | ||||
| import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||||
| import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||
| import static org.mockito.Mockito.times; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
|  | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.quartz.SchedulerException; | ||||
| import org.springframework.context.annotation.Import; | ||||
| import cn.iocoder.dashboard.BaseDbUnitTest; | ||||
| import cn.iocoder.dashboard.framework.quartz.core.scheduler.SchedulerManager; | ||||
| import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.InfJobCreateReqVO; | ||||
| import cn.iocoder.dashboard.modules.infra.convert.job.InfJobConvert; | ||||
| import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO; | ||||
| import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; | ||||
| import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; | ||||
| import cn.iocoder.dashboard.modules.infra.service.job.impl.InfJobServiceImpl; | ||||
|  | ||||
| /** | ||||
|  * {@link InfJobServiceImpl} 的单元测试 | ||||
|  * | ||||
|  * @author neilz | ||||
|  */ | ||||
| @Import(InfJobServiceImpl.class) | ||||
| public class InfJobServiceTest extends BaseDbUnitTest { | ||||
|     @Resource | ||||
|     private InfJobServiceImpl jobService; | ||||
|  | ||||
|     @Resource | ||||
|     private InfJobMapper jobMapper; | ||||
|     @Resource | ||||
|     private SchedulerManager schedulerManager; | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateJob_success() throws SchedulerException { | ||||
|         // 准备参数 | ||||
|         InfJobCreateReqVO reqVO = randomPojo(InfJobCreateReqVO.class); | ||||
|         reqVO.setCronExpression("0 0/1 * * * ? *"); | ||||
|  | ||||
|         // 调用 | ||||
|         Long jobId = jobService.createJob(reqVO); | ||||
|  | ||||
|         // 断言 | ||||
|         assertNotNull(jobId); | ||||
|  | ||||
|         // 校验记录的属性是否正确 | ||||
|         InfJobDO job = jobMapper.selectById(jobId); | ||||
|         assertPojoEquals(reqVO, job); | ||||
|         assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus()); | ||||
|  | ||||
|         // 校验调用 | ||||
|         verify(jobMapper, times(1)).selectByHandlerName(reqVO.getHandlerName()); | ||||
|  | ||||
|         InfJobDO insertJob = InfJobConvert.INSTANCE.convert(reqVO); | ||||
|         insertJob.setStatus(InfJobStatusEnum.INIT.getStatus()); | ||||
|         fillJobMonitorTimeoutEmpty(insertJob); | ||||
|         verify(jobMapper, times(1)).insert(insertJob); | ||||
|  | ||||
|         verify(schedulerManager, times(1)).addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), | ||||
|                 job.getRetryCount(), job.getRetryInterval()); | ||||
|  | ||||
|         InfJobDO updateObj = InfJobDO.builder().id(insertJob.getId()).status(InfJobStatusEnum.NORMAL.getStatus()).build(); | ||||
|         verify(jobMapper, times(1)).updateById(updateObj); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private static void fillJobMonitorTimeoutEmpty(InfJobDO job) { | ||||
|         if (job.getMonitorTimeout() == null) { | ||||
|             job.setMonitorTimeout(0); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,6 +1,7 @@ | ||||
| -- inf 开头的 DB | ||||
| DELETE FROM "inf_config"; | ||||
| DELETE FROM "inf_file"; | ||||
| DELETE FROM "inf_job"; | ||||
|  | ||||
| -- sys 开头的 DB | ||||
| DELETE FROM "sys_dept"; | ||||
|   | ||||
| @@ -29,6 +29,24 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( | ||||
|     PRIMARY KEY ("id") | ||||
| ) COMMENT '文件表'; | ||||
|  | ||||
| CREATE TABLE "inf_job" ( | ||||
|    "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '任务编号', | ||||
|    "name" varchar(32) NOT NULL COMMENT '任务名称', | ||||
|    "status" tinyint(4) NOT NULL COMMENT '任务状态', | ||||
|    "handler_name" varchar(64) NOT NULL COMMENT '处理器的名字', | ||||
|    "handler_param" varchar(255) DEFAULT NULL COMMENT '处理器的参数', | ||||
|    "cron_expression" varchar(32) NOT NULL COMMENT 'CRON 表达式', | ||||
|    "retry_count" int(11) NOT NULL DEFAULT '0' COMMENT '重试次数', | ||||
|    "retry_interval" int(11) NOT NULL DEFAULT '0' COMMENT '重试间隔', | ||||
|    "monitor_timeout" int(11) NOT NULL DEFAULT '0' COMMENT '监控超时时间', | ||||
|    "creator" varchar(64) DEFAULT '' COMMENT '创建者', | ||||
|    "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||||
|    "updater" varchar(64) DEFAULT '' COMMENT '更新者', | ||||
|    "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | ||||
|    "deleted" bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', | ||||
|    PRIMARY KEY ("id") | ||||
| ) COMMENT='定时任务表'; | ||||
|  | ||||
| -- sys 开头的 DB | ||||
|  | ||||
| CREATE TABLE IF NOT EXISTS "sys_dept" ( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 neilz
					neilz