Skip to content

Commit 0aae4e9

Browse files
committed
Deprecate StepRunner
Resolves #4921
1 parent 4ae4051 commit 0aae4e9

File tree

4 files changed

+98
-15
lines changed

4 files changed

+98
-15
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/JobOperatorTestUtils.java

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
1822
import org.springframework.batch.core.job.AbstractJob;
1923
import org.springframework.batch.core.job.Job;
2024
import org.springframework.batch.core.job.JobExecution;
2125
import org.springframework.batch.core.job.SimpleJob;
26+
import org.springframework.batch.core.job.UnexpectedJobExecutionException;
2227
import org.springframework.batch.core.job.flow.FlowJob;
2328
import org.springframework.batch.core.job.parameters.JobParameters;
2429
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
30+
import org.springframework.batch.core.job.parameters.JobParametersInvalidException;
2531
import org.springframework.batch.core.launch.JobOperator;
32+
import org.springframework.batch.core.launch.NoSuchJobException;
33+
import org.springframework.batch.core.listener.JobExecutionListener;
34+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
35+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
2636
import org.springframework.batch.core.repository.JobRepository;
37+
import org.springframework.batch.core.repository.JobRestartException;
2738
import org.springframework.batch.core.step.Step;
2839
import org.springframework.batch.core.step.StepLocator;
2940
import org.springframework.batch.item.ExecutionContext;
@@ -62,6 +73,11 @@
6273
@SuppressWarnings("removal")
6374
public class JobOperatorTestUtils extends JobLauncherTestUtils {
6475

76+
/**
77+
* Name of the single-step job surrounding steps when tested individually
78+
*/
79+
public static final String JOB_NAME = "TestJob";
80+
6581
protected JobOperator jobOperator;
6682

6783
/**
@@ -123,9 +139,9 @@ public JobExecution startJob(JobParameters jobParameters) throws Exception {
123139

124140
/**
125141
* Start just the specified step in a surrounding single-step job of type
126-
* {@link SimpleJob} named {@link StepRunner#JOB_NAME}. A unique set of JobParameters
127-
* will automatically be generated. An IllegalStateException is thrown if there is no
128-
* Step with the given name.
142+
* {@link SimpleJob} named {@link #JOB_NAME}. A unique set of JobParameters will
143+
* automatically be generated. An IllegalStateException is thrown if there is no Step
144+
* with the given name.
129145
* @param stepName The name of the step to launch
130146
* @return JobExecution
131147
*/
@@ -134,13 +150,13 @@ public JobExecution startStep(String stepName) {
134150
}
135151

136152
/**
137-
* Launch just the specified step in a surrounding single-step job of type
138-
* {@link SimpleJob} named {@link StepRunner#JOB_NAME}. An IllegalStateException is
153+
* Extract the step from the injected job and start it in a surrounding single-step
154+
* job of type {@link SimpleJob} named {@link #JOB_NAME}. An IllegalStateException is
139155
* thrown if there is no Step with the given name.
140-
* @param stepName The name of the step to launch
141-
* @param jobParameters The JobParameters to use during the launch
156+
* @param stepName The name of the step to start
157+
* @param jobParameters The JobParameters to use during the start
142158
* @param jobExecutionContext An ExecutionContext whose values will be loaded into the
143-
* Job ExecutionContext prior to launching the step.
159+
* Job ExecutionContext before starting the step.
144160
* @return JobExecution
145161
*/
146162
public JobExecution startStep(String stepName, JobParameters jobParameters, ExecutionContext jobExecutionContext) {
@@ -156,7 +172,62 @@ public JobExecution startStep(String stepName, JobParameters jobParameters, Exec
156172
if (step == null) {
157173
throw new IllegalStateException("No Step found with name: [" + stepName + "]");
158174
}
159-
return getStepRunner().launchStep(step, jobParameters, jobExecutionContext);
175+
176+
return startStep(step, jobParameters, jobExecutionContext);
177+
}
178+
179+
/**
180+
* Start just the specified step with a unique set of job parameters in a surrounding
181+
* single-step job of type {@link SimpleJob} named {@link StepRunner#JOB_NAME}. An
182+
* IllegalStateException is thrown if there is no Step with the given name.
183+
* @param step The step to start
184+
* @return JobExecution
185+
*/
186+
public JobExecution startStep(Step step) {
187+
return startStep(step, getUniqueJobParameters(), new ExecutionContext());
188+
}
189+
190+
/**
191+
* Start just the specified step in a surrounding single-step job of type
192+
* {@link SimpleJob} named {@link StepRunner#JOB_NAME}. An IllegalStateException is
193+
* thrown if there is no Step with the given name.
194+
* @param step The step to start
195+
* @param jobParameters The JobParameters to use during the start
196+
* @param jobExecutionContext An ExecutionContext whose values will be loaded into the
197+
* Job ExecutionContext before starting the step.
198+
* @return JobExecution
199+
*/
200+
public JobExecution startStep(Step step, JobParameters jobParameters, ExecutionContext jobExecutionContext) {
201+
// Create a fake job
202+
SimpleJob job = new SimpleJob();
203+
job.setName(JOB_NAME);
204+
job.setJobRepository(this.jobRepository);
205+
206+
List<Step> stepsToExecute = new ArrayList<>();
207+
stepsToExecute.add(step);
208+
job.setSteps(stepsToExecute);
209+
210+
// Dump the given Job ExecutionContext using a listener
211+
if (jobExecutionContext != null && !jobExecutionContext.isEmpty()) {
212+
job.setJobExecutionListeners(new JobExecutionListener[] { new JobExecutionListener() {
213+
@Override
214+
public void beforeJob(JobExecution jobExecution) {
215+
ExecutionContext jobContext = jobExecution.getExecutionContext();
216+
for (Map.Entry<String, Object> entry : jobExecutionContext.entrySet()) {
217+
jobContext.put(entry.getKey(), entry.getValue());
218+
}
219+
}
220+
} });
221+
}
222+
223+
// Launch the job
224+
try {
225+
return this.jobOperator.start(job, jobParameters);
226+
}
227+
catch (NoSuchJobException | JobExecutionAlreadyRunningException | JobRestartException
228+
| JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
229+
throw new UnexpectedJobExecutionException("Step runner encountered exception.", e);
230+
}
160231
}
161232

162233
/**

spring-batch-test/src/main/java/org/springframework/batch/test/StepRunner.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@
6363
* @author Mahmoud Ben Hassine
6464
* @since 2.0
6565
* @see SimpleJob
66+
* @deprecated since 6.0 in favor of
67+
* {@link JobOperatorTestUtils#startStep(String, JobParameters, ExecutionContext)}.
68+
* Scheduled for removal in 6.2 or later
6669
*/
6770
@SuppressWarnings("removal")
71+
@Deprecated(since = "6.0", forRemoval = true)
6872
public class StepRunner {
6973

7074
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import static org.junit.jupiter.api.Assertions.*;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
1919

2020
import org.junit.jupiter.api.AfterEach;
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323
import org.springframework.batch.core.BatchStatus;
24+
import org.springframework.batch.core.launch.JobOperator;
2425
import org.springframework.batch.core.step.Step;
25-
import org.springframework.batch.core.launch.JobLauncher;
2626
import org.springframework.batch.core.repository.JobRepository;
2727
import org.springframework.beans.BeansException;
2828
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,20 +38,20 @@ class SampleStepTests implements ApplicationContextAware {
3838
@Autowired
3939
private JdbcTemplate jdbcTemplate;
4040

41-
private StepRunner stepRunner;
41+
private JobOperatorTestUtils jobOperatorTestUtils;
4242

4343
private ApplicationContext context;
4444

4545
@Autowired
46-
private JobLauncher jobLauncher;
46+
private JobOperator jobOperator;
4747

4848
@Autowired
4949
private JobRepository jobRepository;
5050

5151
@BeforeEach
5252
void setUp() {
5353
jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
54-
stepRunner = new StepRunner(jobLauncher, jobRepository);
54+
jobOperatorTestUtils = new JobOperatorTestUtils(jobOperator, jobRepository);
5555
}
5656

5757
@AfterEach
@@ -62,7 +62,7 @@ void tearDown() {
6262
@Test
6363
void testTasklet() {
6464
Step step = context.getBean("s2", Step.class);
65-
assertEquals(BatchStatus.COMPLETED, stepRunner.launchStep(step).getStatus());
65+
assertEquals(BatchStatus.COMPLETED, jobOperatorTestUtils.startStep(step).getStatus());
6666
assertEquals(2, jdbcTemplate.queryForObject("SELECT ID from TESTS where NAME = 'SampleTasklet2'", Integer.class)
6767
.intValue());
6868
}

spring-batch-test/src/test/resources/simple-job-launcher-context.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
<property name="jobRepository" ref="jobRepository" />
1515
</bean>
1616

17+
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
18+
19+
<bean id="jobOperator"
20+
class="org.springframework.batch.core.launch.support.TaskExecutorJobOperator">
21+
<property name="jobRepository" ref="jobRepository" />
22+
<property name="jobRegistry" ref="jobRegistry" />
23+
</bean>
24+
1725
<bean id="jobRepository"
1826
class="org.springframework.batch.core.repository.support.JdbcJobRepositoryFactoryBean">
1927
<property name="dataSource" ref="dataSource" />

0 commit comments

Comments
 (0)