Skip to content

Commit 1527593

Browse files
committed
Refine contribution #4218
* Update Javadoc of SpringBatchTest about job autowiring * Apply Spring code style conventions
1 parent 3bb7ce5 commit 1527593

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* {@link JobLauncherTestUtils} if there is a unique job bean.
2828
*
2929
* @author Henning Pöttker
30+
* @author Mahmoud Ben Hassine
3031
* @since 5.0
3132
*/
3233
public class BatchTestContextBeanPostProcessor implements BeanPostProcessor {
@@ -41,7 +42,7 @@ public void setJobProvider(ObjectProvider<Job> jobProvider) {
4142
@Override
4243
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
4344
if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) {
44-
jobProvider.ifUnique(jobLauncherTestUtils::setJob);
45+
this.jobProvider.ifUnique(jobLauncherTestUtils::setJob);
4546
}
4647
return bean;
4748
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
* }
121121
* </pre>
122122
*
123+
* It should be noted that if the test context contains a single job bean definition, that
124+
* is the job under test, then this annotation will set that job in the
125+
* {@link JobLauncherTestUtils} automatically.
126+
*
123127
* @author Mahmoud Ben Hassine
124128
* @since 4.1
125129
* @see JobLauncherTestUtils
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.batch.core.JobExecution;
2525
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2626
import org.springframework.batch.test.JobLauncherTestUtils;
27-
import org.springframework.beans.factory.ObjectProvider;
2827
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2928
import org.springframework.context.annotation.Bean;
3029
import org.springframework.context.annotation.Configuration;
@@ -38,48 +37,49 @@
3837

3938
/**
4039
* @author Henning Pöttker
40+
* @author Mahmoud Ben Hassine
4141
*/
42-
class BatchTestContextBeanPostProcessorTest {
42+
class BatchTestContextBeanPostProcessorTests {
4343

4444
private GenericApplicationContext applicationContext;
4545

4646
@BeforeEach
4747
void setUp() {
48-
applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class);
49-
applicationContext.registerBean(JobLauncherTestUtils.class);
48+
this.applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class);
49+
this.applicationContext.registerBean(JobLauncherTestUtils.class);
5050
}
5151

5252
@AfterEach
5353
void tearDown() {
54-
if (applicationContext != null) {
55-
applicationContext.close();
54+
if (this.applicationContext != null) {
55+
this.applicationContext.close();
5656
}
5757
}
5858

5959
@Test
6060
void testContextWithoutJobBean() {
61-
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
61+
var jobLauncherTestUtils = this.applicationContext.getBean(JobLauncherTestUtils.class);
6262
assertNotNull(jobLauncherTestUtils);
6363
assertNull(jobLauncherTestUtils.getJob());
6464
}
6565

6666
@Test
6767
void testContextWithUniqueJobBean() {
68-
applicationContext.registerBean(MockJob.class);
69-
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
68+
applicationContext.registerBean(StubJob.class);
69+
var jobLauncherTestUtils = this.applicationContext.getBean(JobLauncherTestUtils.class);
7070
assertNotNull(jobLauncherTestUtils.getJob());
7171
}
7272

7373
@Test
7474
void testContextWithTwoJobBeans() {
75-
applicationContext.registerBean("jobA", MockJob.class);
76-
applicationContext.registerBean("jobB", MockJob.class);
75+
this.applicationContext.registerBean("jobA", StubJob.class);
76+
this.applicationContext.registerBean("jobB", StubJob.class);
7777
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
7878
assertNotNull(jobLauncherTestUtils);
7979
assertNull(jobLauncherTestUtils.getJob());
8080
}
8181

82-
static class MockJob implements Job {
82+
static class StubJob implements Job {
8383

8484
@Override
8585
public String getName() {
@@ -100,7 +100,7 @@ static class BatchConfiguration {
100100
DataSource dataSource() {
101101
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
102102
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
103-
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
103+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
104104
}
105105

106106
@Bean

0 commit comments

Comments
 (0)