Refine contribution #4218
* Update Javadoc of SpringBatchTest about job autowiring * Apply Spring code style conventions
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
* {@link JobLauncherTestUtils} if there is a unique job bean.
|
||||
*
|
||||
* @author Henning Pöttker
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 5.0
|
||||
*/
|
||||
public class BatchTestContextBeanPostProcessor implements BeanPostProcessor {
|
||||
@@ -41,7 +42,7 @@ public class BatchTestContextBeanPostProcessor implements BeanPostProcessor {
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) {
|
||||
jobProvider.ifUnique(jobLauncherTestUtils::setJob);
|
||||
this.jobProvider.ifUnique(jobLauncherTestUtils::setJob);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@@ -120,6 +120,10 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* It should be noted that if the test context contains a single job bean definition, that
|
||||
* is the job under test, then this annotation will set that job in the
|
||||
* {@link JobLauncherTestUtils} automatically.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.1
|
||||
* @see JobLauncherTestUtils
|
||||
|
||||
@@ -24,7 +24,6 @@ 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;
|
||||
@@ -38,48 +37,49 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* @author Henning Pöttker
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
class BatchTestContextBeanPostProcessorTest {
|
||||
class BatchTestContextBeanPostProcessorTests {
|
||||
|
||||
private GenericApplicationContext applicationContext;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class);
|
||||
applicationContext.registerBean(JobLauncherTestUtils.class);
|
||||
this.applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class);
|
||||
this.applicationContext.registerBean(JobLauncherTestUtils.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (applicationContext != null) {
|
||||
applicationContext.close();
|
||||
if (this.applicationContext != null) {
|
||||
this.applicationContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testContextWithoutJobBean() {
|
||||
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
|
||||
var jobLauncherTestUtils = this.applicationContext.getBean(JobLauncherTestUtils.class);
|
||||
assertNotNull(jobLauncherTestUtils);
|
||||
assertNull(jobLauncherTestUtils.getJob());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testContextWithUniqueJobBean() {
|
||||
applicationContext.registerBean(MockJob.class);
|
||||
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
|
||||
applicationContext.registerBean(StubJob.class);
|
||||
var jobLauncherTestUtils = this.applicationContext.getBean(JobLauncherTestUtils.class);
|
||||
assertNotNull(jobLauncherTestUtils.getJob());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testContextWithTwoJobBeans() {
|
||||
applicationContext.registerBean("jobA", MockJob.class);
|
||||
applicationContext.registerBean("jobB", MockJob.class);
|
||||
this.applicationContext.registerBean("jobA", StubJob.class);
|
||||
this.applicationContext.registerBean("jobB", StubJob.class);
|
||||
var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class);
|
||||
assertNotNull(jobLauncherTestUtils);
|
||||
assertNull(jobLauncherTestUtils.getJob());
|
||||
}
|
||||
|
||||
static class MockJob implements Job {
|
||||
static class StubJob implements Job {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
@@ -100,7 +100,7 @@ class BatchTestContextBeanPostProcessorTest {
|
||||
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();
|
||||
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
Reference in New Issue
Block a user