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

@@ -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() {