Skip to content

Commit 86784b6

Browse files
committed
Introduce support for a custom reason in @⁠DisabledInAotMode
Closes gh-33833
1 parent fdb763e commit 86784b6

File tree

51 files changed

+198
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+198
-105
lines changed

spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotMode.java

+15
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@
5959
@Documented
6060
@ExtendWith(DisabledInAotModeCondition.class)
6161
public @interface DisabledInAotMode {
62+
63+
/**
64+
* Custom reason to document why the test class or test method is disabled in
65+
* AOT mode.
66+
* <p>If a custom reason is not supplied, the default reason will be used:
67+
* {@code "Disabled in Spring AOT mode"}.
68+
* <p>If a custom reason is supplied, it will be combined with the default
69+
* reason. For example,
70+
* {@code @DisabledInAotMode("@ContextHierarchy is not supported")} will result
71+
* in a combined reason like the following:
72+
* {@code "Disabled in Spring AOT mode ==> @ContextHierarchy is not supported"}.
73+
* @since 6.2
74+
*/
75+
String value() default "";
76+
6277
}

spring-test/src/main/java/org/springframework/test/context/aot/DisabledInAotModeCondition.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,22 @@
1616

1717
package org.springframework.test.context.aot;
1818

19+
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.AnnotatedElement;
21+
import java.util.Optional;
22+
1923
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2024
import org.junit.jupiter.api.extension.ExecutionCondition;
2125
import org.junit.jupiter.api.extension.ExtensionContext;
2226

2327
import org.springframework.aot.AotDetector;
28+
import org.springframework.core.annotation.AnnotatedElementUtils;
2429

2530
/**
26-
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode}.
31+
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode @DisabledInAotMode}.
2732
*
2833
* @author Stephane Nicoll
34+
* @author Sam Brannen
2935
* @since 6.1.2
3036
*/
3137
class DisabledInAotModeCondition implements ExecutionCondition {
@@ -34,9 +40,18 @@ class DisabledInAotModeCondition implements ExecutionCondition {
3440
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
3541
boolean aotEnabled = AotDetector.useGeneratedArtifacts();
3642
if (aotEnabled) {
37-
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode");
43+
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
44+
String customReason = findMergedAnnotation(element, DisabledInAotMode.class)
45+
.map(DisabledInAotMode::value).orElse(null);
46+
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode", customReason);
3847
}
39-
return ConditionEvaluationResult.enabled("Spring AOT mode disabled");
48+
return ConditionEvaluationResult.enabled("Spring AOT mode is not enabled");
49+
}
50+
51+
private static <A extends Annotation> Optional<A> findMergedAnnotation(
52+
AnnotatedElement element, Class<A> annotationType) {
53+
54+
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
4055
}
4156

4257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.aot;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.platform.testkit.engine.EngineTestKit;
21+
22+
import org.springframework.aot.AotDetector;
23+
24+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
25+
import static org.junit.platform.testkit.engine.EventConditions.container;
26+
import static org.junit.platform.testkit.engine.EventConditions.event;
27+
import static org.junit.platform.testkit.engine.EventConditions.skippedWithReason;
28+
29+
/**
30+
* Tests for {@link DisabledInAotMode @DisabledInAotMode}.
31+
*
32+
* @author Sam Brannen
33+
* @since 6.2
34+
*/
35+
class DisabledInAotModeTests {
36+
37+
@Test
38+
void defaultDisabledReason() {
39+
runTestsInAotMode(DefaultReasonTestCase.class, "Disabled in Spring AOT mode");
40+
}
41+
42+
@Test
43+
void customDisabledReason() {
44+
runTestsInAotMode(CustomReasonTestCase.class, "Disabled in Spring AOT mode ==> @ContextHierarchy is not supported in AOT");
45+
}
46+
47+
48+
private static void runTestsInAotMode(Class<?> testClass, String expectedReason) {
49+
try {
50+
System.setProperty(AotDetector.AOT_ENABLED, "true");
51+
52+
EngineTestKit.engine("junit-jupiter")
53+
.selectors(selectClass(testClass))
54+
.execute()
55+
.allEvents()
56+
.assertThatEvents().haveExactly(1,
57+
event(container(testClass.getSimpleName()), skippedWithReason(expectedReason)));
58+
}
59+
finally {
60+
System.clearProperty(AotDetector.AOT_ENABLED);
61+
}
62+
}
63+
64+
65+
@DisabledInAotMode
66+
static class DefaultReasonTestCase {
67+
68+
@Test
69+
void test() {
70+
}
71+
}
72+
73+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
74+
static class CustomReasonTestCase {
75+
76+
@Test
77+
void test() {
78+
}
79+
}
80+
81+
}

spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @see MockitoSpyBeanAndCircularDependenciesWithLazyResolutionProxyIntegrationTests
3939
*/
4040
@SpringJUnitConfig
41-
@DisabledInAotMode // Circular dependencies cannot be resolved in AOT mode unless a @Lazy resolution proxy is used.
41+
@DisabledInAotMode("Circular dependencies cannot be resolved in AOT mode unless a @Lazy resolution proxy is used")
4242
class MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests {
4343

4444
@MockitoSpyBean

spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndContextHierarchyChildIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @see MockitoBeanAndContextHierarchyParentIntegrationTests
4444
*/
4545
@ContextHierarchy(@ContextConfiguration)
46-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
46+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
4747
public class MockitoSpyBeanAndContextHierarchyChildIntegrationTests extends
4848
MockitoBeanAndContextHierarchyParentIntegrationTests {
4949

spring-test/src/test/java/org/springframework/test/context/configuration/interfaces/ContextHierarchyInterfaceTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 4.3
3232
*/
3333
@ExtendWith(SpringExtension.class)
34-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
34+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
3535
class ContextHierarchyInterfaceTests implements ContextHierarchyTestInterface {
3636

3737
@Autowired

spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileInClasspathTestPropertySourceTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @since 4.1
2828
*/
2929
@TestPropertySource("explicit.properties")
30-
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
31-
@DisabledInAotMode
30+
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
3231
public class ExplicitPropertiesFileInClasspathTestPropertySourceTests extends AbstractExplicitPropertiesFileTests {
3332
}

spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileTestPropertySourceTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
* @since 5.2
3636
*/
3737
@DisplayName("Explicit properties file in @TestPropertySource")
38-
// Since Spring test's AOT processing support does not invoke test lifecycle methods such
39-
// as @BeforeAll/@AfterAll, this test class simply is not supported for AOT processing.
40-
@DisabledInAotMode
38+
@DisabledInAotMode("Spring test's AOT processing support does not invoke lifecycle methods such as @BeforeAll/@AfterAll")
4139
class ExplicitPropertiesFileTestPropertySourceTests {
4240

4341
static final String CURRENT_TEST_PACKAGE = "current.test.package";

spring-test/src/test/java/org/springframework/test/context/env/InheritedRelativePathPropertiesFileTestPropertySourceTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
* @author Sam Brannen
2828
* @since 4.1
2929
*/
30-
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
31-
@DisabledInAotMode
30+
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
3231
class InheritedRelativePathPropertiesFileTestPropertySourceTests extends
3332
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
3433

spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesOverriddenByInlinedPropertiesTestPropertySourceTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
* @since 4.1
3333
*/
3434
@TestPropertySource(properties = { "explicit = inlined", "extended = inlined1", "extended = inlined2" })
35-
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
36-
@DisabledInAotMode
35+
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
3736
class MergedPropertiesFilesOverriddenByInlinedPropertiesTestPropertySourceTests extends
3837
MergedPropertiesFilesTestPropertySourceTests {
3938

spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesTestPropertySourceTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
* @since 4.1
3232
*/
3333
@TestPropertySource("extended.properties")
34-
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
35-
@DisabledInAotMode
34+
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
3635
class MergedPropertiesFilesTestPropertySourceTests extends
3736
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
3837

spring-test/src/test/java/org/springframework/test/context/env/subpackage/SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
* @author Sam Brannen
2929
* @since 4.1
3030
*/
31-
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
32-
@DisabledInAotMode
31+
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
3332
class SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests extends
3433
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
3534

spring-test/src/test/java/org/springframework/test/context/expression/ExpressionUsageTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @author Dave Syer
3333
*/
3434
@SpringJUnitConfig
35-
@DisabledInAotMode // SpEL is not supported in AOT
35+
@DisabledInAotMode("SpEL is not supported in AOT")
3636
class ExpressionUsageTests {
3737

3838
@Autowired

spring-test/src/test/java/org/springframework/test/context/hierarchies/meta/MetaHierarchyLevelOneTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
*/
3232
@ExtendWith(SpringExtension.class)
3333
@MetaMetaContextHierarchyConfig
34-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
34+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
3535
class MetaHierarchyLevelOneTests {
3636

3737
@Autowired

spring-test/src/test/java/org/springframework/test/context/hierarchies/meta/MetaHierarchyLevelTwoTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
*/
3636
@ContextConfiguration
3737
@ActiveProfiles("prod")
38-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
38+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
3939
class MetaHierarchyLevelTwoTests extends MetaHierarchyLevelOneTests {
4040

4141
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/ClassHierarchyWithMergedConfigLevelOneTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@
4141
@ContextConfiguration(name = "parent", classes = ClassHierarchyWithMergedConfigLevelOneTests.AppConfig.class),//
4242
@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelOneTests.UserConfig.class) //
4343
})
44-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
44+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
4545
class ClassHierarchyWithMergedConfigLevelOneTests {
4646

4747
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/ClassHierarchyWithMergedConfigLevelTwoTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
*/
3636
@ExtendWith(SpringExtension.class)
3737
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelTwoTests.OrderConfig.class))
38-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
38+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
3939
class ClassHierarchyWithMergedConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {
4040

4141
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/ClassHierarchyWithOverriddenConfigLevelTwoTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
*/
3636
@ExtendWith(SpringExtension.class)
3737
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithOverriddenConfigLevelTwoTests.TestUserConfig.class, inheritLocations = false))
38-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
38+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
3939
class ClassHierarchyWithOverriddenConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {
4040

4141
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/DirtiesContextWithContextHierarchyTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@
5252
@ContextConfiguration(classes = DirtiesContextWithContextHierarchyTests.ChildConfig.class)
5353
})
5454
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
55-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
55+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
5656
class DirtiesContextWithContextHierarchyTests {
5757

5858
@Autowired

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/SingleTestClassWithSingleLevelContextHierarchyTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636
*/
3737
@ExtendWith(SpringExtension.class)
3838
@ContextHierarchy(@ContextConfiguration)
39-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
39+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
4040
class SingleTestClassWithSingleLevelContextHierarchyTests {
4141

4242
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
@ContextHierarchy({
3939
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests.ParentConfig.class),
4040
@ContextConfiguration("SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests-ChildConfig.xml") })
41-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
41+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
4242
class SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests {
4343

4444
@Configuration

spring-test/src/test/java/org/springframework/test/context/hierarchies/standard/SingleTestClassWithTwoLevelContextHierarchyTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
@ContextHierarchy({
3939
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ParentConfig.class),
4040
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ChildConfig.class) })
41-
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
41+
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
4242
public class SingleTestClassWithTwoLevelContextHierarchyTests {
4343

4444
@Configuration

0 commit comments

Comments
 (0)