Skip to content

Commit 791cfd5

Browse files
committed
Polish 0c50079
This commit adds a test case for building a flow job with a job-scoped step. Issue #857
1 parent 0c50079 commit 791cfd5

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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,6 +15,8 @@
1515
*/
1616
package org.springframework.batch.core.job.builder;
1717

18+
import java.util.Arrays;
19+
1820
import static org.junit.Assert.assertEquals;
1921

2022
import org.junit.Before;
@@ -25,19 +27,33 @@
2527
import org.springframework.batch.core.JobExecution;
2628
import org.springframework.batch.core.JobInterruptedException;
2729
import org.springframework.batch.core.JobParameters;
30+
import org.springframework.batch.core.JobParametersBuilder;
31+
import org.springframework.batch.core.Step;
2832
import org.springframework.batch.core.StepExecution;
2933
import org.springframework.batch.core.UnexpectedJobExecutionException;
34+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
35+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
36+
import org.springframework.batch.core.configuration.annotation.JobScope;
37+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
3038
import org.springframework.batch.core.job.flow.Flow;
3139
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
3240
import org.springframework.batch.core.job.flow.JobExecutionDecider;
41+
import org.springframework.batch.core.launch.JobLauncher;
3342
import org.springframework.batch.core.repository.JobRepository;
3443
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
3544
import org.springframework.batch.core.step.StepSupport;
45+
import org.springframework.batch.item.support.ListItemReader;
46+
import org.springframework.beans.factory.annotation.Value;
47+
import org.springframework.context.ApplicationContext;
48+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
49+
import org.springframework.context.annotation.Bean;
50+
import org.springframework.context.annotation.Configuration;
3651
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3752
import org.springframework.lang.Nullable;
3853

3954
/**
4055
* @author Dave Syer
56+
* @author Mahmoud Ben Hassine
4157
*
4258
*/
4359
public class FlowJobBuilderTests {
@@ -238,4 +254,45 @@ public void testBuildWithStopAndRestart() throws Exception {
238254
assertEquals("step2", execution.getStepExecutions().iterator().next().getStepName());
239255
}
240256

257+
@Test
258+
public void testBuildWithJobScopedStep() throws Exception {
259+
// given
260+
ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class);
261+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
262+
Job job = context.getBean(Job.class);
263+
JobParameters jobParameters = new JobParametersBuilder()
264+
.addLong("chunkSize", 2L)
265+
.toJobParameters();
266+
267+
// when
268+
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
269+
270+
// then
271+
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
272+
}
273+
274+
@EnableBatchProcessing
275+
@Configuration
276+
static class JobConfiguration {
277+
278+
@Bean
279+
@JobScope
280+
public Step step(StepBuilderFactory stepBuilderFactory,
281+
@Value("#{jobParameters['chunkSize']}") Integer chunkSize) {
282+
return stepBuilderFactory.get("step")
283+
.<Integer, Integer>chunk(chunkSize)
284+
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4)))
285+
.writer(items -> {})
286+
.build();
287+
}
288+
289+
@Bean
290+
public Job job(JobBuilderFactory jobBuilderFactory) {
291+
return jobBuilderFactory.get("job")
292+
.flow(step(null, null))
293+
.build()
294+
.build();
295+
}
296+
}
297+
241298
}

0 commit comments

Comments
 (0)