Improve @SpringBatchTest to autowire the job under test in JobLauncherTestUtils if it is unique

Issue #4218
This commit is contained in:
Henning Poettker
2022-10-23 22:15:44 +02:00
committed by Mahmoud Ben Hassine
parent 621ec92586
commit 3bb7ce532b
34 changed files with 201 additions and 102 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,9 +49,8 @@ class AMQPJobFunctionalTests {
private JobExplorer jobExplorer;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
void testLaunchJob() throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
this.jobLauncherTestUtils.launchJob();
// when

View File

@@ -18,7 +18,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -31,10 +30,7 @@ class BeanWrapperMapperSampleJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
// when
this.jobLauncherTestUtils.launchJob();

View File

@@ -28,7 +28,6 @@ import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -62,8 +61,7 @@ class CompositeItemWriterSampleFunctionalTests {
}
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");

View File

@@ -29,7 +29,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -82,8 +81,7 @@ class CustomerFilterJobFunctionalTests {
}
@Test
void testFilterJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testFilterJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))),

View File

@@ -21,7 +21,6 @@ import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -57,8 +56,7 @@ class DatabaseShutdownFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Thread.sleep(1000);

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.person.PersonService;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,8 +36,7 @@ class DelegatingJobFunctionalTests {
private PersonService personService;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
assertTrue(personService.getReturnedCount() > 0);

View File

@@ -19,7 +19,6 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -43,8 +42,7 @@ class FootballJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");
jobLauncherTestUtils.launchJob();

View File

@@ -21,7 +21,6 @@ import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -58,8 +57,7 @@ class GracefulShutdownFunctionalTests {
private JobOperator jobOperator;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis())
.toJobParameters();

View File

@@ -23,7 +23,6 @@ import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -43,8 +42,7 @@ public class GroovyJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
assertFalse(new File("target/groovyJob/output/files.zip").exists());
jobLauncherTestUtils.launchJob();
assertTrue(new File("target/groovyJob/output/files.zip").exists());

View File

@@ -20,7 +20,6 @@ import java.io.FileReader;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -45,8 +44,7 @@ class HeaderFooterSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile()));

View File

@@ -25,7 +25,6 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
@@ -92,8 +91,7 @@ class HibernateFailureJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
validatePreConditions();
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -45,8 +44,7 @@ class LoopFlowSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(10, itemWriter.getItems().size());

View File

@@ -25,7 +25,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandler;
import org.springframework.batch.sample.domain.mail.internal.TestMailSender;
@@ -96,8 +95,7 @@ class MailJobFunctionalTests {
}
@Test
void testSkip(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testSkip() throws Exception {
this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 });
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

View File

@@ -19,7 +19,6 @@ package org.springframework.batch.sample;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
@@ -44,8 +43,7 @@ class MultilineJobFunctionalTests {
private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream(), "UTF-8"),
System.getProperty("line.separator"), ""));

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
@@ -41,8 +40,7 @@ class MultilineOrderJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testJobLaunch(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testJobLaunch() throws Exception {
this.jobLauncherTestUtils.launchJob();
Path expectedFile = new ClassPathResource(EXPECTED).getFile().toPath();
Path actualFile = new FileSystemResource(ACTUAL).getFile().toPath();

View File

@@ -21,7 +21,6 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,8 +45,7 @@ class ParallelJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
JobExecution execution = jobLauncherTestUtils.launchJob();
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");

View File

@@ -24,7 +24,6 @@ import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
@@ -64,8 +63,7 @@ class PartitionFileJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -24,7 +24,6 @@ import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
@@ -64,8 +63,7 @@ class PartitionJdbcJobFunctionalTests implements ApplicationContextAware {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
"Define a prototype bean called 'outputTestReader' to check the output");

View File

@@ -16,7 +16,6 @@
package org.springframework.batch.sample;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -24,7 +23,6 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.Chunk;
@@ -54,8 +52,7 @@ class RestartFileSampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void runTest(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void runTest() throws Exception {
JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters();
JobExecution je1 = jobLauncherTestUtils.launchJob(jobParameters);

View File

@@ -21,7 +21,6 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
@@ -70,8 +69,7 @@ class RestartFunctionalTests {
* @throws Exception
*/
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
JobExecution jobExecution = runJobForRestartTest();

View File

@@ -17,7 +17,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.internal.GeneratingTradeItemReader;
import org.springframework.batch.sample.support.RetrySampleItemWriter;
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -48,8 +47,7 @@ class RetrySampleFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
// items processed = items read + 2 exceptions
assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter());

View File

@@ -19,7 +19,6 @@ package org.springframework.batch.sample;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -37,8 +36,7 @@ class TaskletJobFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils
.launchJob(new JobParametersBuilder().addString("value", "foo").toJobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

View File

@@ -30,7 +30,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -82,8 +81,7 @@ class TradeJobFunctionalTests {
}
@Test
void testLaunchJob(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testLaunchJob() throws Exception {
this.jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)),

View File

@@ -21,7 +21,6 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
@@ -65,9 +64,7 @@ abstract class AbstractIoSampleTests {
* Check the resulting credits correspond to inputs increased by fixed amount.
*/
@Test
void testUpdateCredit(@Autowired Job job) throws Exception {
this.jobLauncherTestUtils.setJob(job);
void testUpdateCredit() throws Exception {
open(reader);
List<CustomerCredit> inputs = getCredits(reader);
close(reader);

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
@@ -49,10 +48,7 @@ class MultiLineFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
// when
this.jobLauncherTestUtils.launchJob();

View File

@@ -22,7 +22,6 @@ import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
@@ -49,10 +48,7 @@ class MultiRecordTypeFunctionalTests {
* Output should be the same as input
*/
@Test
void testJob(@Autowired Job job) throws Exception {
// given
this.jobLauncherTestUtils.setJob(job);
void testJob() throws Exception {
// when
jobLauncherTestUtils.launchJob();

View File

@@ -7,4 +7,5 @@
<context:annotation-config/>
<bean class="org.springframework.batch.test.JobLauncherTestUtils"/>
<bean class="org.springframework.batch.test.context.BatchTestContextBeanPostProcessor"/>
</beans>

View File

@@ -0,0 +1,49 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.test.context;
import org.springframework.batch.core.Job;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* {@link BeanPostProcessor} implementation that injects a job bean into
* {@link JobLauncherTestUtils} if there is a unique job bean.
*
* @author Henning Pöttker
* @since 5.0
*/
public class BatchTestContextBeanPostProcessor implements BeanPostProcessor {
private ObjectProvider<Job> jobProvider;
@Autowired
public void setJobProvider(ObjectProvider<Job> jobProvider) {
this.jobProvider = jobProvider;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) {
jobProvider.ifUnique(jobLauncherTestUtils::setJob);
}
return bean;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-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.
@@ -39,6 +39,8 @@ public class BatchTestContextCustomizer implements ContextCustomizer {
private static final String JOB_REPOSITORY_TEST_UTILS_BEAN_NAME = "jobRepositoryTestUtils";
private static final String BATCH_TEST_CONTEXT_BEAN_POST_PROCESSOR_BEAN_NAME = "batchTestContextBeanPostProcessor";
@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
@@ -50,6 +52,8 @@ public class BatchTestContextCustomizer implements ContextCustomizer {
new RootBeanDefinition(JobLauncherTestUtils.class));
registry.registerBeanDefinition(JOB_REPOSITORY_TEST_UTILS_BEAN_NAME,
new RootBeanDefinition(JobRepositoryTestUtils.class));
registry.registerBeanDefinition(BATCH_TEST_CONTEXT_BEAN_POST_PROCESSOR_BEAN_NAME,
new RootBeanDefinition(BatchTestContextBeanPostProcessor.class));
}
@Override

View File

@@ -69,9 +69,6 @@ public class SpringBatchTestJUnit4Tests {
@Autowired
private ItemReader<String> jobScopedItemReader;
@Autowired
private Job jobUnderTest;
@Before
public void setUp() {
this.jobRepositoryTestUtils.removeJobExecutions();
@@ -105,9 +102,6 @@ public class SpringBatchTestJUnit4Tests {
@Test
public void testJob() throws Exception {
// given
this.jobLauncherTestUtils.setJob(this.jobUnderTest);
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();

View File

@@ -19,7 +19,6 @@ import java.util.Arrays;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -72,8 +71,7 @@ public class SpringBatchTestJUnit5Tests {
private ItemReader<String> jobScopedItemReader;
@BeforeEach
void setup(@Autowired Job jobUnderTest) {
this.jobLauncherTestUtils.setJob(jobUnderTest);
void setup() {
this.jobRepositoryTestUtils.removeJobExecutions();
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.test.context;
import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Henning Pöttker
*/
class BatchTestContextBeanPostProcessorTest {
private GenericApplicationContext applicationContext;
@BeforeEach
void setUp() {
applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class);
applicationContext.registerBean(JobLauncherTestUtils.class);
}
@AfterEach
void tearDown() {
if (applicationContext != null) {
applicationContext.close();
}
}
@Test
void testContextWithoutJobBean() {
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
assertNotNull(jobLauncherTestUtils);
assertNull(jobLauncherTestUtils.getJob());
}
@Test
void testContextWithUniqueJobBean() {
applicationContext.registerBean(MockJob.class);
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
assertNotNull(jobLauncherTestUtils.getJob());
}
@Test
void testContextWithTwoJobBeans() {
applicationContext.registerBean("jobA", MockJob.class);
applicationContext.registerBean("jobB", MockJob.class);
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
assertNotNull(jobLauncherTestUtils);
assertNull(jobLauncherTestUtils.getJob());
}
static class MockJob implements Job {
@Override
public String getName() {
return "name";
}
@Override
public void execute(JobExecution execution) {
}
}
@Configuration
@EnableBatchProcessing
static class BatchConfiguration {
@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
}
@Bean
JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
@Bean
BatchTestContextBeanPostProcessor beanPostProcessor() {
return new BatchTestContextBeanPostProcessor();
}
}
}

View File

@@ -46,6 +46,7 @@ class BatchTestContextCustomizerTests {
// then
assertTrue(context.containsBean("jobLauncherTestUtils"));
assertTrue(context.containsBean("jobRepositoryTestUtils"));
assertTrue(context.containsBean("batchTestContextBeanPostProcessor"));
}
@Test

View File

@@ -25,7 +25,6 @@ import io.micrometer.observation.ObservationRegistry;
import io.micrometer.tracing.test.SampleTestRunner;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
@@ -71,11 +70,6 @@ class ObservabilitySampleStepTests extends SampleTestRunner {
return this.observationRegistry;
}
@BeforeEach
void setup(@Autowired Job job) {
this.jobLauncherTestUtils.setJob(job);
}
@AfterEach
@Override
protected void closeMeterRegistry() {