Skip to content

Commit 5b80510

Browse files
committed
Fix JobLauncherTestUtils configuration with SpringBatchTest
Before this commit, the JobLauncherTestUtils was not properly configured when using the SpringBatchTest annotation. In fact, the job launcher was not set at configuration time, which fails with a NPE at runtime. This commit fixes that by updating the BBP used by the annotation. Resolves #5090
1 parent bf282b4 commit 5b80510

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.batch.core.job.Job;
1919
import org.springframework.batch.core.launch.JobOperator;
2020
import org.springframework.batch.core.repository.JobRepository;
21+
import org.springframework.batch.test.JobLauncherTestUtils;
2122
import org.springframework.batch.test.JobOperatorTestUtils;
2223
import org.springframework.batch.test.JobRepositoryTestUtils;
2324
import org.springframework.beans.BeansException;
@@ -67,6 +68,12 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
6768
if (bean instanceof JobRepositoryTestUtils jobRepositoryTestUtils) {
6869
this.jobRepositoryProvider.ifUnique(jobRepositoryTestUtils::setJobRepository);
6970
}
71+
// TODO remove in 6.2 when JobLauncherTestUtils is removed
72+
if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) {
73+
this.jobProvider.ifUnique(jobLauncherTestUtils::setJob);
74+
this.jobRepositoryProvider.ifUnique(jobLauncherTestUtils::setJobRepository);
75+
this.jobOperatorProvider.ifUnique(jobLauncherTestUtils::setJobLauncher);
76+
}
7077
return bean;
7178
}
7279

spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,46 +15,50 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import org.jspecify.annotations.Nullable;
18+
import javax.sql.DataSource;
19+
1920
import org.junit.jupiter.api.Test;
2021

2122
import org.springframework.batch.core.ExitStatus;
23+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2224
import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository;
2325
import org.springframework.batch.core.job.Job;
2426
import org.springframework.batch.core.job.JobExecution;
25-
import org.springframework.batch.core.step.Step;
26-
import org.springframework.batch.core.step.StepContribution;
27-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2827
import org.springframework.batch.core.job.builder.JobBuilder;
29-
import org.springframework.batch.core.launch.JobLauncher;
3028
import org.springframework.batch.core.repository.JobRepository;
31-
import org.springframework.batch.core.scope.context.ChunkContext;
29+
import org.springframework.batch.core.step.Step;
3230
import org.springframework.batch.core.step.builder.StepBuilder;
3331
import org.springframework.batch.core.step.tasklet.Tasklet;
3432
import org.springframework.batch.infrastructure.repeat.RepeatStatus;
35-
import org.springframework.context.ApplicationContext;
36-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
33+
import org.springframework.batch.test.context.SpringBatchTest;
34+
import org.springframework.beans.factory.annotation.Autowired;
3735
import org.springframework.context.annotation.Bean;
3836
import org.springframework.context.annotation.Configuration;
3937
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4038
import org.springframework.jdbc.support.JdbcTransactionManager;
4139

42-
import javax.sql.DataSource;
43-
4440
import static org.junit.jupiter.api.Assertions.assertEquals;
4541

4642
/**
4743
* @author mminella
4844
* @author Mahmoud Ben Hassine
4945
*/
46+
// TODO remove in 6.2 when JobLauncherTestUtils is removed
47+
@SpringBatchTest
5048
class JobLauncherTestUtilsTests {
5149

50+
@Autowired
51+
private JobLauncherTestUtils testUtils;
52+
5253
@Test
53-
void testStepExecutionWithJavaConfig() {
54-
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);
54+
void testJobExecutionWithDeprecatedLauncher() throws Exception {
55+
JobExecution execution = testUtils.launchJob();
5556

56-
JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class);
57+
assertEquals(ExitStatus.COMPLETED, execution.getExitStatus());
58+
}
5759

60+
@Test
61+
void testStepExecutionWithDeprecatedLauncher() {
5862
JobExecution execution = testUtils.launchStep("step1");
5963

6064
assertEquals(ExitStatus.COMPLETED, execution.getExitStatus());
@@ -66,30 +70,14 @@ void testStepExecutionWithJavaConfig() {
6670
static class TestJobConfiguration {
6771

6872
@Bean
69-
public Step step(JobRepository jobRepository) {
70-
return new StepBuilder("step1", jobRepository).tasklet(new Tasklet() {
71-
72-
@Override
73-
public @Nullable RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
74-
throws Exception {
75-
return null;
76-
}
77-
}, transactionManager(dataSource())).build();
73+
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
74+
Tasklet tasklet = (contribution, chunkContext) -> RepeatStatus.FINISHED;
75+
return new StepBuilder("step1", jobRepository).tasklet(tasklet, transactionManager).build();
7876
}
7977

8078
@Bean
81-
public Job job(JobRepository jobRepository) {
82-
return new JobBuilder("job", jobRepository).flow(step(jobRepository)).end().build();
83-
}
84-
85-
@Bean
86-
public JobLauncherTestUtils testUtils(Job jobUnderTest, JobRepository jobRepository, JobLauncher jobLauncher) {
87-
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
88-
jobLauncherTestUtils.setJob(jobUnderTest);
89-
jobLauncherTestUtils.setJobRepository(jobRepository);
90-
jobLauncherTestUtils.setJobLauncher(jobLauncher);
91-
92-
return jobLauncherTestUtils;
79+
public Job job(JobRepository jobRepository, Step step) {
80+
return new JobBuilder("job", jobRepository).start(step).build();
9381
}
9482

9583
@Bean

0 commit comments

Comments
 (0)