From 1527593a3676483fa6aaa7808c1453500dec7964 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 8 Nov 2022 13:45:13 +0100 Subject: [PATCH] Refine contribution #4218 * Update Javadoc of SpringBatchTest about job autowiring * Apply Spring code style conventions --- .../BatchTestContextBeanPostProcessor.java | 3 ++- .../batch/test/context/SpringBatchTest.java | 4 +++ ...tchTestContextBeanPostProcessorTests.java} | 26 +++++++++---------- 3 files changed, 19 insertions(+), 14 deletions(-) rename spring-batch-test/src/test/java/org/springframework/batch/test/context/{BatchTestContextBeanPostProcessorTest.java => BatchTestContextBeanPostProcessorTests.java} (79%) diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java index 56c3f9820..536875504 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java @@ -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; } 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 d9a4c3a45..51cfb1c2f 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 @@ -120,6 +120,10 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; * } * * + * 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 diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTests.java similarity index 79% rename from spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java rename to spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTests.java index 179c46ce5..51c515c0e 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTests.java @@ -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