From 28dcfe4f7bd7940faca8072599a76f63614d5926 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Fri, 3 Jan 2025 22:33:56 +0100 Subject: [PATCH] Replace `spring.factories` with `@ContextCustomizerFactories` --- .../BatchTestContextCustomizerFactory.java | 6 +-- .../batch/test/context/SpringBatchTest.java | 3 ++ .../main/resources/META-INF/spring.factories | 4 -- ...atchTestContextCustomizerFactoryTests.java | 7 ++- .../SpringBatchTestIntegrationTests.java | 48 +++++++++++++++++++ 5 files changed, 56 insertions(+), 12 deletions(-) delete mode 100644 spring-batch-test/src/main/resources/META-INF/spring.factories create mode 100644 spring-batch-test/src/test/java/org/springframework/batch/test/context/SpringBatchTestIntegrationTests.java diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactory.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactory.java index 12625b0dea..d9af41e73c 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactory.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactory.java @@ -26,6 +26,7 @@ * Factory for {@link BatchTestContextCustomizer}. * * @author Mahmoud Ben Hassine + * @author Stefano Cordio * @since 4.1 */ public class BatchTestContextCustomizerFactory implements ContextCustomizerFactory { @@ -33,10 +34,7 @@ public class BatchTestContextCustomizerFactory implements ContextCustomizerFacto @Override public ContextCustomizer createContextCustomizer(Class testClass, List configAttributes) { - if (AnnotatedElementUtils.hasAnnotation(testClass, SpringBatchTest.class)) { - return new BatchTestContextCustomizer(); - } - return null; + return new BatchTestContextCustomizer(); } } 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 a597f95b9b..2b721dfffe 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 @@ -28,6 +28,7 @@ import org.springframework.batch.test.JobRepositoryTestUtils; import org.springframework.batch.test.JobScopeTestExecutionListener; import org.springframework.batch.test.StepScopeTestExecutionListener; +import org.springframework.test.context.ContextCustomizerFactories; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -130,6 +131,7 @@ * * @author Mahmoud Ben Hassine * @author Taeik Lim + * @author Stefano Cordio * @since 4.1 * @see JobLauncherTestUtils * @see JobRepositoryTestUtils @@ -140,6 +142,7 @@ @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited +@ContextCustomizerFactories(BatchTestContextCustomizerFactory.class) @TestExecutionListeners(listeners = { StepScopeTestExecutionListener.class, JobScopeTestExecutionListener.class }, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) @ExtendWith(SpringExtension.class) diff --git a/spring-batch-test/src/main/resources/META-INF/spring.factories b/spring-batch-test/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 98e6e569c6..0000000000 --- a/spring-batch-test/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,4 +0,0 @@ -# Batch ContextCustomizerFactory implementation for the Spring TestContext Framework -# -org.springframework.test.context.ContextCustomizerFactory= \ -org.springframework.batch.test.context.BatchTestContextCustomizerFactory diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactoryTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactoryTests.java index 4c693d05ec..d34e281cb7 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactoryTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerFactoryTests.java @@ -23,8 +23,7 @@ import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextCustomizer; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; /** * @author Mahmoud Ben Hassine @@ -43,7 +42,7 @@ void testCreateContextCustomizer_whenAnnotationIsPresent() { ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes); // then - assertNotNull(contextCustomizer); + assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer); } @Test @@ -56,7 +55,7 @@ void testCreateContextCustomizer_whenAnnotationIsAbsent() { ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes); // then - assertNull(contextCustomizer); + assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer); } @SpringBatchTest diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/SpringBatchTestIntegrationTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/SpringBatchTestIntegrationTests.java new file mode 100644 index 0000000000..26b6038806 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/SpringBatchTestIntegrationTests.java @@ -0,0 +1,48 @@ +package org.springframework.batch.test.context; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertSame; + +/** + * @author Stefano Cordio + */ +@SpringJUnitConfig +@SpringBatchTest +class SpringBatchTestIntegrationTests { + + @Autowired + ApplicationContext context; + + @Nested + class InnerWithoutSpringBatchTest { + + @Autowired + ApplicationContext context; + + @Test + void test() { + assertSame(SpringBatchTestIntegrationTests.this.context, context); + } + + } + + @Nested + @SpringBatchTest + class InnerWithSpringBatchTest { + + @Autowired + ApplicationContext context; + + @Test + void test() { + assertSame(SpringBatchTestIntegrationTests.this.context, context); + } + + } + +}