From f104baa4175410de4cad7768725a0ca1f5d39afe Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Sun, 21 Aug 2022 13:36:06 +0200 Subject: [PATCH] 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 --- .../batch/test/JobRepositoryTestUtils.java | 29 +++++++++++-------- .../batch/test/context/SpringBatchTest.java | 11 +++++-- .../test/JobRepositoryTestUtilsTests.java | 14 +-------- .../test/SpringBatchTestJUnit4Tests.java | 4 +++ .../test/SpringBatchTestJUnit5Tests.java | 8 +++-- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java index 58ef69b695..d577aef4fa 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java @@ -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. @@ -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 @@ -52,7 +53,7 @@ * @author Dave Syer * @author Mahmoud Ben Hassine */ -public class JobRepositoryTestUtils extends AbstractJdbcBatchMetadataDao implements InitializingBean { +public class JobRepositoryTestUtils { private JobRepository jobRepository; @@ -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. @@ -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); } @@ -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 */ @@ -209,4 +210,8 @@ public void removeJobExecutions() throws DataAccessException { } + private String getQuery(String base) { + return StringUtils.replace(base, "%PREFIX%", this.tablePrefix); + } + } diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java index 1ba6c00dc8..352ff604ae 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java @@ -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); * } @@ -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 diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java index c1e500e3fb..c5768a8fed 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java @@ -39,6 +39,7 @@ /** * @author Dave Syer + * @author Mahmoud Ben Hassine * */ @SpringJUnitConfig(locations = "/simple-job-launcher-context.xml") @@ -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); diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java index 338cf2aa2e..4b03feb9ea 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java @@ -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(); } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java index c82a34f1ad..a8c0631bb6 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java @@ -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; @@ -69,7 +70,9 @@ public class SpringBatchTestJUnit5Tests { private ItemReader jobScopedItemReader; @BeforeEach - void setUp() { + void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) { + this.jobLauncherTestUtils.setJob(jobUnderTest); + this.jobRepositoryTestUtils.setDataSource(testDatabase); this.jobRepositoryTestUtils.removeJobExecutions(); } @@ -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