Skip to content

Commit f104baa

Browse files
committed
Remove datasource autowiring in JobRepositoryTestUtils
Before this commit, trying to register a `JobRepositoryTestUtils` bean in a test context that contains multiple datasources fails at startup, because a datasource is autowired in `JobRepositoryTestUtils` while multiple are defined. This commit removes the autowiring of the datasource in JobRepositoryTestUtils. Resolves #4178
1 parent fe40370 commit f104baa

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-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.
@@ -43,6 +43,7 @@
4343
import org.springframework.jdbc.core.RowMapper;
4444
import org.springframework.lang.Nullable;
4545
import org.springframework.util.Assert;
46+
import org.springframework.util.StringUtils;
4647

4748
/**
4849
* Convenience class for creating and removing {@link JobExecution} instances from a
@@ -52,7 +53,7 @@
5253
* @author Dave Syer
5354
* @author Mahmoud Ben Hassine
5455
*/
55-
public class JobRepositoryTestUtils extends AbstractJdbcBatchMetadataDao implements InitializingBean {
56+
public class JobRepositoryTestUtils {
5657

5758
private JobRepository jobRepository;
5859

@@ -69,14 +70,7 @@ public JobParameters getNext(@Nullable JobParameters parameters) {
6970

7071
private JdbcOperations jdbcTemplate;
7172

72-
/**
73-
* @see InitializingBean#afterPropertiesSet()
74-
*/
75-
@Override
76-
public void afterPropertiesSet() throws Exception {
77-
Assert.notNull(jobRepository, "JobRepository must be set");
78-
Assert.notNull(jdbcTemplate, "DataSource must be set");
79-
}
73+
private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX;
8074

8175
/**
8276
* Default constructor.
@@ -90,12 +84,10 @@ public JobRepositoryTestUtils() {
9084
* @param dataSource a {@link DataSource}
9185
*/
9286
public JobRepositoryTestUtils(JobRepository jobRepository, DataSource dataSource) {
93-
super();
9487
this.jobRepository = jobRepository;
9588
setDataSource(dataSource);
9689
}
9790

98-
@Autowired
9991
public final void setDataSource(DataSource dataSource) {
10092
jdbcTemplate = new JdbcTemplate(dataSource);
10193
}
@@ -107,6 +99,15 @@ public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIn
10799
this.jobParametersIncrementer = jobParametersIncrementer;
108100
}
109101

102+
/**
103+
* Set the prefix of batch tables.
104+
* @param tablePrefix of batch tables
105+
* @since 5.0
106+
*/
107+
public void setTablePrefix(String tablePrefix) {
108+
this.tablePrefix = tablePrefix;
109+
}
110+
110111
/**
111112
* @param jobRepository the jobRepository to set
112113
*/
@@ -209,4 +210,8 @@ public void removeJobExecutions() throws DataAccessException {
209210

210211
}
211212

213+
private String getQuery(String base) {
214+
return StringUtils.replace(base, "%PREFIX%", this.tablePrefix);
215+
}
216+
212217
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@
6363
* @Autowired
6464
* private Job jobUnderTest;
6565
*
66+
* @Autowired
67+
* private DataSource testDatabase;
68+
*
6669
* @Before
6770
* public void setup() {
71+
* this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
6872
* this.jobRepositoryTestUtils.removeJobExecutions();
6973
* this.jobLauncherTestUtils.setJob(this.jobUnderTest);
7074
* }
@@ -100,14 +104,15 @@
100104
* private JobRepositoryTestUtils jobRepositoryTestUtils;
101105
*
102106
* @BeforeEach
103-
* public void clearJobExecutions() {
107+
* public void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
108+
* this.jobLauncherTestUtils.setJob(jobUnderTest);
109+
* this.jobRepositoryTestUtils.setDataSource(testDatabase);
104110
* this.jobRepositoryTestUtils.removeJobExecutions();
105111
* }
106112
*
107113
* @Test
108-
* public void testMyJob(@Autowired Job jobUnderTest) throws Exception {
114+
* public void testMyJob() throws Exception {
109115
* // given
110-
* this.jobLauncherTestUtils.setJob(jobUnderTest);
111116
* JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
112117
*
113118
* // when

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
/**
4141
* @author Dave Syer
42+
* @author Mahmoud Ben Hassine
4243
*
4344
*/
4445
@SpringJUnitConfig(locations = "/simple-job-launcher-context.xml")
@@ -65,19 +66,6 @@ void init() {
6566
beforeSteps = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
6667
}
6768

68-
@Test
69-
void testMandatoryProperties() {
70-
utils = new JobRepositoryTestUtils();
71-
assertThrows(IllegalArgumentException.class, utils::afterPropertiesSet);
72-
}
73-
74-
@Test
75-
void testMandatoryDataSource() {
76-
utils = new JobRepositoryTestUtils();
77-
utils.setJobRepository(jobRepository);
78-
assertThrows(IllegalArgumentException.class, utils::afterPropertiesSet);
79-
}
80-
8169
@Test
8270
void testCreateJobExecutions() throws Exception {
8371
utils = new JobRepositoryTestUtils(jobRepository, dataSource);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public class SpringBatchTestJUnit4Tests {
7070
@Autowired
7171
private Job jobUnderTest;
7272

73+
@Autowired
74+
private DataSource testDatabase;
75+
7376
@Before
7477
public void setUp() {
78+
this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
7579
this.jobRepositoryTestUtils.removeJobExecutions();
7680
}
7781

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import javax.sql.DataSource;
2121

22+
import org.junit.jupiter.api.BeforeAll;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425

@@ -69,7 +70,9 @@ public class SpringBatchTestJUnit5Tests {
6970
private ItemReader<String> jobScopedItemReader;
7071

7172
@BeforeEach
72-
void setUp() {
73+
void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
74+
this.jobLauncherTestUtils.setJob(jobUnderTest);
75+
this.jobRepositoryTestUtils.setDataSource(testDatabase);
7376
this.jobRepositoryTestUtils.removeJobExecutions();
7477
}
7578

@@ -88,9 +91,8 @@ void testJobScopedItemReader() throws Exception {
8891
}
8992

9093
@Test
91-
void testJob(@Autowired Job jobUnderTest) throws Exception {
94+
void testJob() throws Exception {
9295
// given
93-
this.jobLauncherTestUtils.setJob(jobUnderTest);
9496
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
9597

9698
// when

0 commit comments

Comments
 (0)