diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestClassScanner.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestClassScanner.java index 8280410eff6..69cc98ff947 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestClassScanner.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestClassScanner.java @@ -17,10 +17,12 @@ package org.springframework.test.context.aot; import java.lang.annotation.Annotation; +import java.lang.reflect.Modifier; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.Comparator; +import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.stream.Stream; @@ -82,8 +84,10 @@ import static org.springframework.core.annotation.MergedAnnotations.SearchStrate class TestClassScanner { // JUnit Jupiter - private static final String EXTEND_WITH_ANNOTATION_NAME = "org.junit.jupiter.api.extension.ExtendWith"; private static final String SPRING_EXTENSION_NAME = "org.springframework.test.context.junit.jupiter.SpringExtension"; + private static final String EXTEND_WITH_ANNOTATION_NAME = "org.junit.jupiter.api.extension.ExtendWith"; + private static final String CLASS_TEMPLATE_ANNOTATION_NAME = "org.junit.jupiter.api.ClassTemplate"; + private static final String NESTED_ANNOTATION_NAME = "org.junit.jupiter.api.Nested"; // JUnit 4 private static final String RUN_WITH_ANNOTATION_NAME = "org.junit.runner.RunWith"; @@ -161,6 +165,7 @@ class TestClassScanner { .map(this::getJavaClass) .flatMap(Optional::stream) .filter(this::isSpringTestClass) + .flatMap(this::expandJupiterClassTemplateIfNecessary) .distinct() .sorted(Comparator.comparing(Class::getName)); } @@ -184,6 +189,45 @@ class TestClassScanner { return isSpringTestClass; } + /** + * Expand the supplied test class into a stream containing the supplied test + * class. If the supplied class is a JUnit Jupiter {@code @ClassTemplate} + * (such as a {@code @ParameterizedClass}), the returned stream will also + * contain {@code @Nested} test classes associated with the supplied test + * class. + * @since 7.0 + */ + private Stream> expandJupiterClassTemplateIfNecessary(Class testClass) { + if (isJupiterClassTemplate(testClass)) { + Set> testClasses = new HashSet<>(); + collectNestedTestClasses(testClass, testClasses, new HashSet<>()); + testClasses.add(testClass); + return testClasses.stream(); + } + return Stream.of(testClass); + } + + /** + * Collect all {@code @Nested} test classes declared in the superclass hierarchy + * of the supplied test class as well as {@code @Nested} test classes declared + * in the supplied test class itself, recursively. + * @since 7.0 + */ + private static void collectNestedTestClasses(Class testClass, Set> testClasses, Set> visited) { + if (visited.add(testClass)) { + Class superclass = testClass.getSuperclass(); + if (superclass != null && superclass != Object.class) { + collectNestedTestClasses(superclass, testClasses, visited); + } + for (Class nestedClass : testClass.getDeclaredClasses()) { + if (isJupiterNestedClass(nestedClass)) { + testClasses.add(nestedClass); + collectNestedTestClasses(nestedClass, testClasses, visited); + } + } + } + } + private static boolean isJupiterSpringTestClass(Class clazz) { return MergedAnnotations.search(TYPE_HIERARCHY) .withEnclosingClasses(ClassUtils::isInnerClass) @@ -195,6 +239,15 @@ class TestClassScanner { .anyMatch(SPRING_EXTENSION_NAME::equals); } + private static boolean isJupiterClassTemplate(Class clazz) { + return MergedAnnotations.from(clazz, TYPE_HIERARCHY).isPresent(CLASS_TEMPLATE_ANNOTATION_NAME); + } + + private static boolean isJupiterNestedClass(Class clazz) { + return (!Modifier.isAbstract(clazz.getModifiers()) && ClassUtils.isInnerClass(clazz) && + MergedAnnotations.from(clazz, TYPE_HIERARCHY).isPresent(NESTED_ANNOTATION_NAME)); + } + private static boolean isJUnit4SpringTestClass(Class clazz) { MergedAnnotation mergedAnnotation = MergedAnnotations.from(clazz, INHERITED_ANNOTATIONS).get(RUN_WITH_ANNOTATION_NAME); diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/AbstractAotTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/AbstractAotTests.java index 5e4d5e9a8ad..cdaf8da4828 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/AbstractAotTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/AbstractAotTests.java @@ -30,61 +30,95 @@ import java.util.stream.Stream; abstract class AbstractAotTests { static final String[] expectedSourceFilesForBasicSpringTests = { + + // BasicSpringJupiterSharedConfigTests -- not generated b/c already generated for AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests. + // BasicSpringJupiterTests -- not generated b/c already generated for AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests. + // BasicSpringJupiterTests.NestedTests -- not generated b/c already generated for BasicSpringJupiterParameterizedClassTests.NestedTests. + // Global "org/springframework/test/context/aot/AotTestContextInitializers__Generated.java", "org/springframework/test/context/aot/AotTestAttributes__Generated.java", - // BasicSpringJupiterImportedConfigTests + + // AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests__TestContext001_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests__TestContext001_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests__TestContext001_ManagementApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests__TestContext001_ManagementBeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext001_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext001_ManagementBeanDefinitions.java", "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext001_BeanDefinitions.java", - // BasicSpringJupiterSharedConfigTests + + // AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.InheritedDoublyNestedTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_BeanFactoryRegistrations.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_ManagementApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_ManagementBeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests_InheritedDoublyNestedTests__TestContext002_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests_InheritedDoublyNestedTests__TestContext002_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests_InheritedDoublyNestedTests__TestContext002_ManagementApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests_InheritedNestedTests_InheritedDoublyNestedTests__TestContext002_ManagementBeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java", "org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext002_BeanDefinitions.java", "org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext002_ManagementBeanDefinitions.java", "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext002_BeanDefinitions.java", - // BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests. - // BasicSpringJupiterTests.NestedTests + + // BasicSpringJupiterImportedConfigTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext003_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext003_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_BeanFactoryRegistrations.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_ManagementApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_ManagementBeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext003_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext003_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext003_BeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext003_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext003_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext003_ManagementBeanDefinitions.java", "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext003_BeanDefinitions.java", - // BasicSpringTestNGTests + + // BasicSpringJupiterParameterizedClassTests.NestedTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext004_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext004_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext004_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext004_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests__TestContext004_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests__TestContext004_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests__TestContext004_ManagementApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests__TestContext004_ManagementBeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext004_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext004_ManagementBeanDefinitions.java", "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext004_BeanDefinitions.java", - // BasicSpringVintageTests + + // BasicSpringJupiterParameterizedClassTests.NestedTests.DoublyNestedTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext005_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext005_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests_DoublyNestedTests__TestContext005_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests_DoublyNestedTests__TestContext005_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests_DoublyNestedTests__TestContext005_ManagementApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests_NestedTests_DoublyNestedTests__TestContext005_ManagementBeanFactoryRegistrations.java", "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext005_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext005_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext005_ManagementBeanDefinitions.java", "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext005_BeanDefinitions.java", - // DisabledInAotRuntimeMethodLevelTests + + // BasicSpringTestNGTests "org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java", "org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext006_ApplicationContextInitializer.java", - "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext006_BeanDefinitions.java", - "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext006_BeanFactoryRegistrations.java", - "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext006_BeanDefinitions.java" + "org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext006_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext006_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext006_BeanDefinitions.java", + "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext006_BeanDefinitions.java", + + // BasicSpringVintageTests + "org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java", + "org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext007_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext007_BeanFactoryRegistrations.java", + "org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext007_BeanDefinitions.java", + "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext007_BeanDefinitions.java", + + // DisabledInAotRuntimeMethodLevelTests + "org/springframework/context/event/DefaultEventListenerFactory__TestContext008_BeanDefinitions.java", + "org/springframework/context/event/EventListenerMethodProcessor__TestContext008_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext008_ApplicationContextInitializer.java", + "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext008_BeanDefinitions.java", + "org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests__TestContext008_BeanFactoryRegistrations.java", + "org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext008_BeanDefinitions.java" }; Stream> scan() { diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java index 386ad48c58a..81634d5bdc6 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java @@ -46,6 +46,7 @@ import org.springframework.context.aot.AbstractAotProcessor; import org.springframework.core.test.tools.CompileWithForkedClassLoader; import org.springframework.core.test.tools.TestCompiler; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests; +import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterParameterizedClassTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests; @@ -116,11 +117,12 @@ class AotIntegrationTests extends AbstractAotTests { // .printFiles(System.out) .compile(compiled -> // AOT RUN-TIME: EXECUTION - runTestsInAotMode(7, List.of( + runTestsInAotMode(17, List.of( // The #s represent how many tests should run from each test class, which // must add up to the expectedNumTests above. /* 1 */ BasicSpringJupiterSharedConfigTests.class, /* 2 */ BasicSpringJupiterTests.class, // NestedTests get executed automatically + /* 2 * 5 */ BasicSpringJupiterParameterizedClassTests.class, // NestedTests get executed automatically // Run @Import tests AFTER the tests with otherwise identical config // in order to ensure that the other test classes are not accidentally // using the config for the @Import tests. @@ -147,6 +149,9 @@ class AotIntegrationTests extends AbstractAotTests { .filter(clazz -> clazz.getSimpleName().endsWith("Tests")) // TestNG EJB tests use @PersistenceContext which is not yet supported in tests in AOT mode. .filter(clazz -> !clazz.getPackageName().contains("testng.transaction.ejb")) + // AOT processing works for ParameterizedDependencyInjectionTests by itself + // but fails for an unknown reason within the entire spring-test module. + .filter(clazz -> !clazz.getName().equals("org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests")) .toList(); // Optionally set failOnError flag to true to halt processing at the first failure. diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java index 9dc27cdf29b..2e2d456305a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests; +import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterParameterizedClassTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests; @@ -55,9 +56,9 @@ class TestAotProcessorTests extends AbstractAotTests { Path classpathRoot = Files.createDirectories(tempDir.resolve("build/classes")); Stream.of( BasicSpringJupiterImportedConfigTests.class, + BasicSpringJupiterParameterizedClassTests.class, BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, - BasicSpringJupiterTests.NestedTests.class, BasicSpringTestNGTests.class, BasicSpringVintageTests.class, DisabledInAotProcessingTests.class, diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestClassScannerTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestClassScannerTests.java index 5032ad18170..59f4c38310c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestClassScannerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestClassScannerTests.java @@ -18,7 +18,9 @@ package org.springframework.test.context.aot; import org.junit.jupiter.api.Test; +import org.springframework.test.context.aot.samples.basic.AbstractSpringJupiterParameterizedClassTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests; +import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterParameterizedClassTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests; import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests; @@ -45,6 +47,11 @@ class TestClassScannerTests extends AbstractAotTests { BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, BasicSpringJupiterTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.InheritedDoublyNestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.DoublyNestedTests.class, BasicSpringTestNGTests.class, BasicSpringVintageTests.class, DisabledInAotProcessingTests.class, @@ -61,6 +68,11 @@ class TestClassScannerTests extends AbstractAotTests { BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, BasicSpringJupiterTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.InheritedDoublyNestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.DoublyNestedTests.class, DisabledInAotProcessingTests.class, DisabledInAotRuntimeClassLevelTests.class, DisabledInAotRuntimeMethodLevelTests.class @@ -87,6 +99,11 @@ class TestClassScannerTests extends AbstractAotTests { BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, BasicSpringJupiterTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.InheritedDoublyNestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.DoublyNestedTests.class, BasicSpringVintageTests.class, BasicSpringTestNGTests.class, DisabledInAotProcessingTests.class, @@ -103,6 +120,11 @@ class TestClassScannerTests extends AbstractAotTests { BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class, BasicSpringJupiterTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.class, + AbstractSpringJupiterParameterizedClassTests.InheritedNestedTests.InheritedDoublyNestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.class, + BasicSpringJupiterParameterizedClassTests.NestedTests.DoublyNestedTests.class, BasicSpringVintageTests.class, DisabledInAotProcessingTests.class, DisabledInAotRuntimeClassLevelTests.class, diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests.java new file mode 100644 index 00000000000..dcee0184d40 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/AbstractSpringJupiterParameterizedClassTests.java @@ -0,0 +1,82 @@ +/* + * Copyright 2002-present 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.test.context.aot.samples.basic; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.aot.samples.common.MessageService; +import org.springframework.test.context.aot.samples.management.ManagementConfiguration; +import org.springframework.test.context.env.YamlTestProperties; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Abstract base test class which declares {@link Nested @Nested} test classes + * that will be inherited by concrete subclasses. + * + * @author Sam Brannen + * @since 7.0 + */ +@SpringJUnitConfig({BasicTestConfiguration.class, ManagementConfiguration.class}) +@TestPropertySource(properties = "test.engine = jupiter") +@YamlTestProperties({ + "classpath:org/springframework/test/context/aot/samples/basic/test1.yaml", + "classpath:org/springframework/test/context/aot/samples/basic/test2.yaml" +}) +public abstract class AbstractSpringJupiterParameterizedClassTests { + + @Nested + public class InheritedNestedTests { + + @Test + void test(@Autowired ApplicationContext context, @Autowired MessageService messageService, + @Value("${test.engine}") String testEngine, @Value("${foo}") String foo) { + + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + assertThat(foo).isEqualTo("${foo}"); + assertThat(testEngine).isEqualTo("jupiter"); + BasicSpringJupiterTests.assertEnvProperties(context); + } + + @Nested + @TestPropertySource(properties = "foo=quux") + public class InheritedDoublyNestedTests { + + @Test + void test(@Autowired ApplicationContext context, @Autowired MessageService messageService, + @Value("${test.engine}") String testEngine, @Value("${foo}") String foo) { + + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + assertThat(foo).isEqualTo("quux"); + assertThat(testEngine).isEqualTo("jupiter"); + BasicSpringJupiterTests.assertEnvProperties(context); + } + } + } + + // This is here to ensure that an inner class is only considered a nested test + // class if it's annotated with @Nested. + public class InheritedNotReallyNestedTests { + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests.java new file mode 100644 index 00000000000..87197b6049b --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterParameterizedClassTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2002-present 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.test.context.aot.samples.basic; + +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedClass; +import org.junit.jupiter.params.provider.ValueSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.aot.samples.common.MessageService; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * {@link ParameterizedClass @ParameterizedClass} variant of {@link BasicSpringJupiterTests}. + * + * @author Sam Brannen + * @since 7.0 + */ +@ParameterizedClass +@ValueSource(strings = {"foo", "bar"}) +public class BasicSpringJupiterParameterizedClassTests extends AbstractSpringJupiterParameterizedClassTests { + + private final String parameterizedString; + + @Resource + Integer magicNumber; + + + BasicSpringJupiterParameterizedClassTests(String parameterizedString) { + this.parameterizedString = parameterizedString; + } + + + @Test + void test(@Autowired ApplicationContext context, @Autowired MessageService messageService, + @Value("${test.engine}") String testEngine) { + assertThat("foo".equals(parameterizedString) || "bar".equals(parameterizedString)).isTrue(); + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); + assertThat(testEngine).isEqualTo("jupiter"); + assertThat(magicNumber).isEqualTo(42); + BasicSpringJupiterTests.assertEnvProperties(context); + } + + @Nested + @TestPropertySource(properties = "foo=bar") + @ActiveProfiles(resolver = SpanishActiveProfilesResolver.class) + public class NestedTests { + + @Test + void test(@Autowired ApplicationContext context, @Autowired MessageService messageService, + @Value("${test.engine}") String testEngine, @Value("${foo}") String foo) { + assertThat("foo".equals(parameterizedString) || "bar".equals(parameterizedString)).isTrue(); + assertThat(messageService.generateMessage()).isEqualTo("¡Hola, AOT!"); + assertThat(foo).isEqualTo("bar"); + assertThat(testEngine).isEqualTo("jupiter"); + BasicSpringJupiterTests.assertEnvProperties(context); + } + + @Nested + @TestPropertySource(properties = "foo=quux") + public class DoublyNestedTests { + + @Test + void test(@Autowired ApplicationContext context, @Autowired MessageService messageService, + @Value("${test.engine}") String testEngine, @Value("${foo}") String foo) { + assertThat("foo".equals(parameterizedString) || "bar".equals(parameterizedString)).isTrue(); + assertThat(messageService.generateMessage()).isEqualTo("¡Hola, AOT!"); + assertThat(foo).isEqualTo("quux"); + assertThat(testEngine).isEqualTo("jupiter"); + BasicSpringJupiterTests.assertEnvProperties(context); + } + } + } + + // This is here to ensure that an inner class is only considered a nested test + // class if it's annotated with @Nested. + public class NotReallyNestedTests { + } + +}