Skip to content

Commit 6e239d5

Browse files
committed
Update Spring Batch to upstream API changes
Fix Spring Batch tests following upstream changes related to Spring Batch issue 4130. Closes gh-32237
1 parent 127d320 commit 6e239d5

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunnerTests.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ void incrementExistingExecution() {
9898
@Test
9999
void retryFailedExecution() {
100100
this.contextRunner.run((context) -> {
101+
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
101102
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
102103
Job job = jobLauncherContext.jobBuilder()
103-
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build())
104+
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
105+
.transactionManager(transactionManager).build())
104106
.incrementer(new RunIdIncrementer()).build();
105107
jobLauncherContext.runner.execute(job, new JobParameters());
106108
jobLauncherContext.runner.execute(job, new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
@@ -111,9 +113,10 @@ void retryFailedExecution() {
111113
@Test
112114
void runDifferentInstances() {
113115
this.contextRunner.run((context) -> {
116+
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
114117
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
115-
Job job = jobLauncherContext.jobBuilder()
116-
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build()).build();
118+
Job job = jobLauncherContext.jobBuilder().start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
119+
.transactionManager(transactionManager).build()).build();
117120
// start a job instance
118121
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo").toJobParameters();
119122
jobLauncherContext.runner.execute(job, jobParameters);
@@ -128,9 +131,11 @@ void runDifferentInstances() {
128131
@Test
129132
void retryFailedExecutionOnNonRestartableJob() {
130133
this.contextRunner.run((context) -> {
134+
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
131135
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
132136
Job job = jobLauncherContext.jobBuilder().preventRestart()
133-
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build())
137+
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
138+
.transactionManager(transactionManager).build())
134139
.incrementer(new RunIdIncrementer()).build();
135140
jobLauncherContext.runner.execute(job, new JobParameters());
136141
jobLauncherContext.runner.execute(job, new JobParameters());
@@ -149,9 +154,11 @@ void retryFailedExecutionOnNonRestartableJob() {
149154
@Test
150155
void retryFailedExecutionWithNonIdentifyingParameters() {
151156
this.contextRunner.run((context) -> {
157+
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
152158
JobLauncherApplicationRunnerContext jobLauncherContext = new JobLauncherApplicationRunnerContext(context);
153159
Job job = jobLauncherContext.jobBuilder()
154-
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet()).build())
160+
.start(jobLauncherContext.stepBuilder().tasklet(throwingTasklet())
161+
.transactionManager(transactionManager).build())
155162
.incrementer(new RunIdIncrementer()).build();
156163
JobParameters jobParameters = new JobParametersBuilder().addLong("id", 1L, false).addLong("foo", 2L, false)
157164
.toJobParameters();
@@ -187,9 +194,11 @@ static class JobLauncherApplicationRunnerContext {
187194
JobLauncherApplicationRunnerContext(ApplicationContext context) {
188195
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
189196
JobRepository jobRepository = context.getBean(JobRepository.class);
197+
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
190198
this.jobs = new JobBuilderFactory(jobRepository);
191-
this.steps = new StepBuilderFactory(jobRepository, context.getBean(PlatformTransactionManager.class));
192-
this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null).build();
199+
this.steps = new StepBuilderFactory(jobRepository);
200+
this.step = this.steps.get("step").tasklet((contribution, chunkContext) -> null)
201+
.transactionManager(transactionManager).build();
193202
this.job = this.jobs.get("job").start(this.step).build();
194203
this.jobExplorer = context.getBean(JobExplorer.class);
195204
this.runner = new JobLauncherApplicationRunner(jobLauncher, this.jobExplorer, jobRepository);

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-batch/src/main/java/smoketest/batch/SampleBatchApplication.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,36 @@
1818

1919
import org.springframework.batch.core.Job;
2020
import org.springframework.batch.core.Step;
21-
import org.springframework.batch.core.StepContribution;
2221
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2322
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
2423
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
25-
import org.springframework.batch.core.scope.context.ChunkContext;
2624
import org.springframework.batch.core.step.tasklet.Tasklet;
2725
import org.springframework.batch.repeat.RepeatStatus;
28-
import org.springframework.beans.factory.annotation.Autowired;
2926
import org.springframework.boot.SpringApplication;
3027
import org.springframework.boot.autoconfigure.SpringBootApplication;
3128
import org.springframework.context.annotation.Bean;
29+
import org.springframework.transaction.PlatformTransactionManager;
3230

3331
@SpringBootApplication
3432
@EnableBatchProcessing
3533
public class SampleBatchApplication {
3634

37-
@Autowired
3835
private JobBuilderFactory jobs;
3936

40-
@Autowired
4137
private StepBuilderFactory steps;
4238

43-
@Bean
44-
protected Tasklet tasklet() {
39+
private PlatformTransactionManager transactionManager;
4540

46-
return new Tasklet() {
47-
@Override
48-
public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
49-
return RepeatStatus.FINISHED;
50-
}
51-
};
41+
public SampleBatchApplication(JobBuilderFactory jobs, StepBuilderFactory steps,
42+
PlatformTransactionManager transactionManager) {
43+
this.jobs = jobs;
44+
this.steps = steps;
45+
this.transactionManager = transactionManager;
46+
}
5247

48+
@Bean
49+
protected Tasklet tasklet() {
50+
return (contribution, context) -> RepeatStatus.FINISHED;
5351
}
5452

5553
@Bean
@@ -59,7 +57,7 @@ public Job job() {
5957

6058
@Bean
6159
protected Step step1() {
62-
return this.steps.get("step1").tasklet(tasklet()).build();
60+
return this.steps.get("step1").tasklet(tasklet()).transactionManager(this.transactionManager).build();
6361
}
6462

6563
public static void main(String[] args) {

0 commit comments

Comments
 (0)