Skip to content

Commit

Permalink
Remove datasource autowiring in JobRepositoryTestUtils
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Aug 21, 2022
1 parent fe40370 commit f104baa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Convenience class for creating and removing {@link JobExecution} instances from a
Expand All @@ -52,7 +53,7 @@
* @author Dave Syer
* @author Mahmoud Ben Hassine
*/
public class JobRepositoryTestUtils extends AbstractJdbcBatchMetadataDao implements InitializingBean {
public class JobRepositoryTestUtils {

private JobRepository jobRepository;

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

private JdbcOperations jdbcTemplate;

/**
* @see InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jobRepository, "JobRepository must be set");
Assert.notNull(jdbcTemplate, "DataSource must be set");
}
private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX;

/**
* Default constructor.
Expand All @@ -90,12 +84,10 @@ public JobRepositoryTestUtils() {
* @param dataSource a {@link DataSource}
*/
public JobRepositoryTestUtils(JobRepository jobRepository, DataSource dataSource) {
super();
this.jobRepository = jobRepository;
setDataSource(dataSource);
}

@Autowired
public final void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
Expand All @@ -107,6 +99,15 @@ public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIn
this.jobParametersIncrementer = jobParametersIncrementer;
}

/**
* Set the prefix of batch tables.
* @param tablePrefix of batch tables
* @since 5.0
*/
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}

/**
* @param jobRepository the jobRepository to set
*/
Expand Down Expand Up @@ -209,4 +210,8 @@ public void removeJobExecutions() throws DataAccessException {

}

private String getQuery(String base) {
return StringUtils.replace(base, "%PREFIX%", this.tablePrefix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@
* @Autowired
* private Job jobUnderTest;
*
* @Autowired
* private DataSource testDatabase;
*
* @Before
* public void setup() {
* this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
* this.jobRepositoryTestUtils.removeJobExecutions();
* this.jobLauncherTestUtils.setJob(this.jobUnderTest);
* }
Expand Down Expand Up @@ -100,14 +104,15 @@
* private JobRepositoryTestUtils jobRepositoryTestUtils;
*
* @BeforeEach
* public void clearJobExecutions() {
* public void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
* this.jobLauncherTestUtils.setJob(jobUnderTest);
* this.jobRepositoryTestUtils.setDataSource(testDatabase);
* this.jobRepositoryTestUtils.removeJobExecutions();
* }
*
* @Test
* public void testMyJob(@Autowired Job jobUnderTest) throws Exception {
* public void testMyJob() throws Exception {
* // given
* this.jobLauncherTestUtils.setJob(jobUnderTest);
* JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
*
* // when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
@SpringJUnitConfig(locations = "/simple-job-launcher-context.xml")
Expand All @@ -65,19 +66,6 @@ void init() {
beforeSteps = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
}

@Test
void testMandatoryProperties() {
utils = new JobRepositoryTestUtils();
assertThrows(IllegalArgumentException.class, utils::afterPropertiesSet);
}

@Test
void testMandatoryDataSource() {
utils = new JobRepositoryTestUtils();
utils.setJobRepository(jobRepository);
assertThrows(IllegalArgumentException.class, utils::afterPropertiesSet);
}

@Test
void testCreateJobExecutions() throws Exception {
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ public class SpringBatchTestJUnit4Tests {
@Autowired
private Job jobUnderTest;

@Autowired
private DataSource testDatabase;

@Before
public void setUp() {
this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
this.jobRepositoryTestUtils.removeJobExecutions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.sql.DataSource;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -69,7 +70,9 @@ public class SpringBatchTestJUnit5Tests {
private ItemReader<String> jobScopedItemReader;

@BeforeEach
void setUp() {
void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
this.jobLauncherTestUtils.setJob(jobUnderTest);
this.jobRepositoryTestUtils.setDataSource(testDatabase);
this.jobRepositoryTestUtils.removeJobExecutions();
}

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

@Test
void testJob(@Autowired Job jobUnderTest) throws Exception {
void testJob() throws Exception {
// given
this.jobLauncherTestUtils.setJob(jobUnderTest);
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();

// when
Expand Down

0 comments on commit f104baa

Please sign in to comment.