Skip to content

Commit 05bbd3f

Browse files
committed
Remove dependency autowiring in test utilities
Issue #4233
1 parent 842d08c commit 05bbd3f

File tree

9 files changed

+65
-39
lines changed

9 files changed

+65
-39
lines changed

spring-batch-samples/src/test/java/org/springframework/batch/sample/config/JobRunnerConfiguration.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.sample.config;
1717

18+
import org.springframework.batch.core.launch.JobLauncher;
19+
import org.springframework.batch.core.repository.JobRepository;
1820
import org.springframework.batch.test.JobLauncherTestUtils;
1921
import org.springframework.context.annotation.Bean;
2022
import org.springframework.context.annotation.Configuration;
@@ -28,8 +30,11 @@
2830
public class JobRunnerConfiguration {
2931

3032
@Bean
31-
public JobLauncherTestUtils utils() throws Exception {
32-
return new JobLauncherTestUtils();
33+
public JobLauncherTestUtils utils(JobRepository jobRepository, JobLauncher jobLauncher) {
34+
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
35+
jobLauncherTestUtils.setJobRepository(jobRepository);
36+
jobLauncherTestUtils.setJobLauncher(jobLauncher);
37+
return jobLauncherTestUtils;
3338
}
3439

3540
}

spring-batch-samples/src/test/resources/org/springframework/batch/sample/JobStepFunctionalTests-context.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77

88
<bean class="org.springframework.batch.test.JobLauncherTestUtils">
99
<property name="job" ref="jobStepJob"/>
10+
<property name="jobRepository" ref="jobRepository"/>
11+
<property name="jobLauncher" ref="jobLauncher"/>
1012
</bean>
1113
</beans>

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25+
2526
import org.springframework.batch.core.Job;
2627
import org.springframework.batch.core.JobExecution;
2728
import org.springframework.batch.core.JobParameter;
@@ -35,7 +36,6 @@
3536
import org.springframework.batch.core.repository.JobRepository;
3637
import org.springframework.batch.core.step.StepLocator;
3738
import org.springframework.batch.item.ExecutionContext;
38-
import org.springframework.beans.factory.annotation.Autowired;
3939
import org.springframework.context.ApplicationContext;
4040
import org.springframework.lang.Nullable;
4141

@@ -94,7 +94,6 @@ public void setJob(Job job) {
9494
* The {@link JobRepository} to use for creating new {@link JobExecution} instances.
9595
* @param jobRepository a {@link JobRepository}
9696
*/
97-
@Autowired
9897
public void setJobRepository(JobRepository jobRepository) {
9998
this.jobRepository = jobRepository;
10099
}
@@ -117,7 +116,6 @@ public Job getJob() {
117116
* A {@link JobLauncher} instance that can be used to launch jobs.
118117
* @param jobLauncher a job launcher
119118
*/
120-
@Autowired
121119
public void setJobLauncher(JobLauncher jobLauncher) {
122120
this.jobLauncher = jobLauncher;
123121
}

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

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

18-
import java.sql.ResultSet;
19-
import java.sql.SQLException;
2018
import java.util.ArrayList;
2119
import java.util.Collection;
2220
import java.util.Collections;
2321
import java.util.List;
2422

25-
import javax.sql.DataSource;
26-
2723
import org.springframework.batch.core.JobExecution;
2824
import org.springframework.batch.core.JobInstance;
2925
import org.springframework.batch.core.JobParameter;
@@ -34,16 +30,7 @@
3430
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
3531
import org.springframework.batch.core.repository.JobRepository;
3632
import org.springframework.batch.core.repository.JobRestartException;
37-
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
38-
import org.springframework.beans.factory.InitializingBean;
39-
import org.springframework.beans.factory.annotation.Autowired;
40-
import org.springframework.dao.DataAccessException;
41-
import org.springframework.jdbc.core.JdbcOperations;
42-
import org.springframework.jdbc.core.JdbcTemplate;
43-
import org.springframework.jdbc.core.RowMapper;
4433
import org.springframework.lang.Nullable;
45-
import org.springframework.util.Assert;
46-
import org.springframework.util.StringUtils;
4734

4835
/**
4936
* Convenience class for creating and removing {@link JobExecution} instances from a
@@ -92,7 +79,6 @@ public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIn
9279
/**
9380
* @param jobRepository the jobRepository to set
9481
*/
95-
@Autowired
9682
public void setJobRepository(JobRepository jobRepository) {
9783
this.jobRepository = jobRepository;
9884
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
package org.springframework.batch.test.context;
1717

1818
import org.springframework.batch.core.Job;
19+
import org.springframework.batch.core.launch.JobLauncher;
20+
import org.springframework.batch.core.repository.JobRepository;
1921
import org.springframework.batch.test.JobLauncherTestUtils;
22+
import org.springframework.batch.test.JobRepositoryTestUtils;
2023
import org.springframework.beans.BeansException;
2124
import org.springframework.beans.factory.ObjectProvider;
2225
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,15 +37,34 @@ public class BatchTestContextBeanPostProcessor implements BeanPostProcessor {
3437

3538
private ObjectProvider<Job> jobProvider;
3639

40+
private ObjectProvider<JobRepository> jobRepositoryProvider;
41+
42+
private ObjectProvider<JobLauncher> jobLauncherProvider;
43+
3744
@Autowired
3845
public void setJobProvider(ObjectProvider<Job> jobProvider) {
3946
this.jobProvider = jobProvider;
4047
}
4148

49+
@Autowired
50+
public void setJobRepositoryProvider(ObjectProvider<JobRepository> jobRepositoryProvider) {
51+
this.jobRepositoryProvider = jobRepositoryProvider;
52+
}
53+
54+
@Autowired
55+
public void setJobLauncherProvider(ObjectProvider<JobLauncher> jobLauncherProvider) {
56+
this.jobLauncherProvider = jobLauncherProvider;
57+
}
58+
4259
@Override
4360
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
4461
if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) {
4562
this.jobProvider.ifUnique(jobLauncherTestUtils::setJob);
63+
this.jobRepositoryProvider.ifUnique(jobLauncherTestUtils::setJobRepository);
64+
this.jobLauncherProvider.ifUnique(jobLauncherTestUtils::setJobLauncher);
65+
}
66+
if (bean instanceof JobRepositoryTestUtils jobRepositoryTestUtils) {
67+
this.jobRepositoryProvider.ifUnique(jobRepositoryTestUtils::setJobRepository);
4668
}
4769
return bean;
4870
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@
3535
* Annotation that can be specified on a test class that runs Spring Batch based tests.
3636
* Provides the following features over the regular <em>Spring TestContext Framework</em>:
3737
* <ul>
38-
* <li>Registers a {@link JobLauncherTestUtils} bean with the
39-
* {@link BatchTestContextCustomizer#JOB_LAUNCHER_TEST_UTILS_BEAN_NAME} which can be used
40-
* in tests for launching jobs and steps.</li>
41-
* <li>Registers a {@link JobRepositoryTestUtils} bean with the
42-
* {@link BatchTestContextCustomizer#JOB_REPOSITORY_TEST_UTILS_BEAN_NAME} which can be
43-
* used in tests setup to create or remove job executions.</li>
38+
* <li>Registers a {@link JobLauncherTestUtils} bean named "jobLauncherTestUtils" which
39+
* can be used in tests for launching jobs and steps.</li>
40+
* <li>Registers a {@link JobRepositoryTestUtils} bean named "jobRepositoryTestUtils"
41+
* which can be used in tests setup to create or remove job executions.</li>
4442
* <li>Registers the {@link StepScopeTestExecutionListener} and
4543
* {@link JobScopeTestExecutionListener} as test execution listeners which are required to
4644
* test step/job scoped beans.</li>
4745
* </ul>
4846
* <p>
49-
* A typical usage of this annotation with JUnit 4 is like:
47+
* A typical usage of this annotation with JUnit 4 is like the following:
5048
*
5149
* <pre class="code">
5250
* &#064;RunWith(SpringRunner.class)
@@ -66,7 +64,7 @@
6664
* &#064;Before
6765
* public void setup() {
6866
* this.jobRepositoryTestUtils.removeJobExecutions();
69-
* this.jobLauncherTestUtils.setJob(this.jobUnderTest);
67+
* this.jobLauncherTestUtils.setJob(this.jobUnderTest); // this is optional if the job is unique
7068
* }
7169
*
7270
* &#064;Test
@@ -84,9 +82,9 @@
8482
* }
8583
* </pre>
8684
*
87-
* For JUnit 5, this annotation can be used without having to manually register the
85+
* For JUnit 5, this annotation can be used without manually registering the
8886
* {@link SpringExtension} since {@code @SpringBatchTest} is meta-annotated with
89-
* {@code @ExtendWith(SpringExtension.class)}:
87+
* {@code @ExtendWith(SpringExtension.class)}. Here is an example:
9088
*
9189
* <pre class="code">
9290
* &#064;SpringBatchTest
@@ -101,7 +99,7 @@
10199
*
102100
* &#064;BeforeEach
103101
* public void setup(@Autowired Job jobUnderTest) {
104-
* this.jobLauncherTestUtils.setJob(jobUnderTest);
102+
* this.jobLauncherTestUtils.setJob(jobUnderTest); // this is optional if the job is unique
105103
* this.jobRepositoryTestUtils.removeJobExecutions();
106104
* }
107105
*
@@ -124,6 +122,12 @@
124122
* is the job under test, then this annotation will set that job in the
125123
* {@link JobLauncherTestUtils} automatically.
126124
*
125+
* <strong>The test context must contain a <code>JobRepository</code> and a
126+
* <code>JobLauncher</code> beans for this annotation to properly set up test utilities.
127+
* In the previous example, the imported configuration class
128+
* <code>MyBatchJobConfiguration</code> is expected to have such beans defined in it (or
129+
* imported from another configuration class). </strong>
130+
*
127131
* @author Mahmoud Ben Hassine
128132
* @since 4.1
129133
* @see JobLauncherTestUtils

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import org.springframework.batch.core.StepContribution;
2626
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2727
import org.springframework.batch.core.job.builder.JobBuilder;
28+
import org.springframework.batch.core.launch.JobLauncher;
2829
import org.springframework.batch.core.repository.JobRepository;
2930
import org.springframework.batch.core.scope.context.ChunkContext;
3031
import org.springframework.batch.core.step.builder.StepBuilder;
3132
import org.springframework.batch.core.step.tasklet.Tasklet;
3233
import org.springframework.batch.repeat.RepeatStatus;
33-
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.context.ApplicationContext;
3535
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3636
import org.springframework.context.annotation.Bean;
@@ -97,9 +97,11 @@ public Job job(JobRepository jobRepository) {
9797
}
9898

9999
@Bean
100-
public JobLauncherTestUtils testUtils(Job jobUnderTest) {
100+
public JobLauncherTestUtils testUtils(Job jobUnderTest, JobRepository jobRepository, JobLauncher jobLauncher) {
101101
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
102102
jobLauncherTestUtils.setJob(jobUnderTest);
103+
jobLauncherTestUtils.setJobRepository(jobRepository);
104+
jobLauncherTestUtils.setJobLauncher(jobLauncher);
103105

104106
return jobLauncherTestUtils;
105107
}

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

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

18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
2018
import java.util.ArrayList;
2119
import java.util.List;
2220

2321
import javax.sql.DataSource;
2422

2523
import org.junit.jupiter.api.Test;
24+
2625
import org.springframework.batch.core.ExitStatus;
2726
import org.springframework.batch.core.Job;
2827
import org.springframework.batch.core.JobExecution;
@@ -33,6 +32,7 @@
3332
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
3433
import org.springframework.batch.core.configuration.annotation.StepScope;
3534
import org.springframework.batch.core.job.builder.JobBuilder;
35+
import org.springframework.batch.core.launch.JobLauncher;
3636
import org.springframework.batch.core.repository.JobRepository;
3737
import org.springframework.batch.core.step.builder.StepBuilder;
3838
import org.springframework.batch.item.Chunk;
@@ -49,6 +49,8 @@
4949
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
5050
import org.springframework.transaction.PlatformTransactionManager;
5151

52+
import static org.junit.jupiter.api.Assertions.assertEquals;
53+
5254
@SpringJUnitConfig
5355
class StepScopeAnnotatedListenerIntegrationTests {
5456

@@ -104,8 +106,11 @@ static class TestConfig {
104106
private PlatformTransactionManager transactionManager;
105107

106108
@Bean
107-
JobLauncherTestUtils jobLauncherTestUtils() {
108-
return new JobLauncherTestUtils();
109+
JobLauncherTestUtils jobLauncherTestUtils(JobRepository jobRepository, JobLauncher jobLauncher) {
110+
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
111+
jobLauncherTestUtils.setJobRepository(jobRepository);
112+
jobLauncherTestUtils.setJobLauncher(jobLauncher);
113+
return jobLauncherTestUtils;
109114
}
110115

111116
@Bean

spring-batch-test/src/test/resources/job-runner-context.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
55
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
66

7-
<context:annotation-config/>
8-
<bean class="org.springframework.batch.test.JobLauncherTestUtils"/>
7+
<bean class="org.springframework.batch.test.JobLauncherTestUtils">
8+
<property name="jobRepository" ref="jobRepository"/>
9+
<property name="jobLauncher" ref="jobLauncher"/>
10+
</bean>
911

1012
</beans>

0 commit comments

Comments
 (0)