You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch单元测试报错:jobExecution不能为null

Spring Batch 5.x 单步骤单元测试报“jobExecution不能为null”错误

我使用spring-boot-starter-batch 3.3.3和spring-batch-test 5.1.2,对包含多个步骤的流式作业中的单个步骤进行单元测试时,出现“jobExecution不能为null”的错误。找不到Spring Batch 5.x版本中launchStep的测试示例,按常规方式编写代码仍报错,代码如下:

JobExecution jobExecution = MetaDataInstanceFactory.createJobExecution();
jobRepository.update(jobExecution);  // 尝试过此操作但无变化
jobLauncherTestUtils.setJobRepository(jobRepository);
ExecutionContext executionContext = jobExecution.getExecutionContext();

jobLauncherTestUtils.setJob(batchRequestJob);
JobExecution result = jobLauncherTestUtils.launchStep(
  "step-1-create",
  Job.jobParameters()
);

报错信息:

jobExecution must not be null
java.lang.IllegalArgumentException: jobExecution must not be null
    at org.springframework.util.Assert.notNull(Assert.java:172)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:286)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:157)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:148)
    at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher.run(TaskExecutorJobLauncher.java:59)
    at org.springframework.batch.test.StepRunner.launchJob(StepRunner.java:175)
    at org.springframework.batch.test.StepRunner.launchStep(StepRunner.java:165)
    at org.springframework.batch.test.JobLauncherTestUtils.launchStep(JobLauncherTestUtils.java:241)
    at org.springframework.batch.test.JobLauncherTestUtils.launchStep(JobLauncherTestUtils.java:214)
    at points.pie.core.configuration.TestBatchJobConfiguration.requestJobCreateStep(TestBatchJobConfiguration.java:225)
...

解决方案

方式一:使用Spring Batch 5推荐的StepTestTemplate

Spring Batch 5官方更推荐用StepTestTemplate测试单个步骤,它能自动处理执行上下文,避免手动管理JobExecution的问题。示例代码:

@Autowired
private StepTestTemplate stepTestTemplate;

@Autowired
private Step step1Create; // 注入要测试的目标步骤

@Test
void testStep1Create() {
    stepTestTemplate.execute(step -> {
        // 按需设置执行上下文参数
        step.getExecutionContext().put("key", "value");
        return step1Create;
    });

    // 断言步骤执行结果
    Assertions.assertEquals(BatchStatus.COMPLETED, stepTestTemplate.getJobExecution().getStatus());
}

方式二:修复JobLauncherTestUtils的使用逻辑

如果坚持使用JobLauncherTestUtils,注意Spring Batch 5中launchStep方法会自动创建并管理JobExecution,手动提前创建的实例会和内部逻辑冲突,需删除手动创建JobExecution的代码。调整后代码:

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Autowired
private Job batchRequestJob;

@Test
void testStep1Create() {
    jobLauncherTestUtils.setJob(batchRequestJob);
    // 直接调用launchStep,无需手动创建JobExecution
    JobExecution result = jobLauncherTestUtils.launchStep(
        "step-1-create",
        new JobParametersBuilder().toJobParameters()
    );
    
    Assertions.assertEquals(BatchStatus.COMPLETED, result.getStatus());
}

关键注意事项

  • 测试类必须添加@SpringBatchTest注解,该注解会自动配置JobLauncherTestUtils、StepTestTemplate等测试所需Bean。
  • 你的依赖版本匹配:spring-boot-starter-batch 3.3.3对应的Spring Batch版本就是5.1.2,版本无冲突。
  • Spring Batch 5中JobLauncherTestUtils的launchStep内部逻辑已更新,无需手动创建和管理JobExecution实例,否则会触发断言错误。

内容的提问来源于stack exchange,提问作者Daniel Manley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 11:33:24