Skip to content

Commit

Permalink
Introduce support for a custom reason in @⁠DisabledInAotMode
Browse files Browse the repository at this point in the history
Closes gh-33833
  • Loading branch information
sbrannen committed Nov 1, 2024
1 parent fdb763e commit 86784b6
Show file tree
Hide file tree
Showing 51 changed files with 198 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@
@Documented
@ExtendWith(DisabledInAotModeCondition.class)
public @interface DisabledInAotMode {

/**
* Custom reason to document why the test class or test method is disabled in
* AOT mode.
* <p>If a custom reason is not supplied, the default reason will be used:
* {@code "Disabled in Spring AOT mode"}.
* <p>If a custom reason is supplied, it will be combined with the default
* reason. For example,
* {@code @DisabledInAotMode("@ContextHierarchy is not supported")} will result
* in a combined reason like the following:
* {@code "Disabled in Spring AOT mode ==> @ContextHierarchy is not supported"}.
* @since 6.2
*/
String value() default "";

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand All @@ -16,16 +16,22 @@

package org.springframework.test.context.aot;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import org.springframework.aot.AotDetector;
import org.springframework.core.annotation.AnnotatedElementUtils;

/**
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode}.
* {@link ExecutionCondition} implementation for {@link DisabledInAotMode @DisabledInAotMode}.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.1.2
*/
class DisabledInAotModeCondition implements ExecutionCondition {
Expand All @@ -34,9 +40,18 @@ class DisabledInAotModeCondition implements ExecutionCondition {
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
boolean aotEnabled = AotDetector.useGeneratedArtifacts();
if (aotEnabled) {
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode");
AnnotatedElement element = context.getElement().orElseThrow(() -> new IllegalStateException("No AnnotatedElement"));
String customReason = findMergedAnnotation(element, DisabledInAotMode.class)
.map(DisabledInAotMode::value).orElse(null);
return ConditionEvaluationResult.disabled("Disabled in Spring AOT mode", customReason);
}
return ConditionEvaluationResult.enabled("Spring AOT mode disabled");
return ConditionEvaluationResult.enabled("Spring AOT mode is not enabled");
}

private static <A extends Annotation> Optional<A> findMergedAnnotation(
AnnotatedElement element, Class<A> annotationType) {

return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2002-2024 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;

import org.junit.jupiter.api.Test;
import org.junit.platform.testkit.engine.EngineTestKit;

import org.springframework.aot.AotDetector;

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.testkit.engine.EventConditions.container;
import static org.junit.platform.testkit.engine.EventConditions.event;
import static org.junit.platform.testkit.engine.EventConditions.skippedWithReason;

/**
* Tests for {@link DisabledInAotMode @DisabledInAotMode}.
*
* @author Sam Brannen
* @since 6.2
*/
class DisabledInAotModeTests {

@Test
void defaultDisabledReason() {
runTestsInAotMode(DefaultReasonTestCase.class, "Disabled in Spring AOT mode");
}

@Test
void customDisabledReason() {
runTestsInAotMode(CustomReasonTestCase.class, "Disabled in Spring AOT mode ==> @ContextHierarchy is not supported in AOT");
}


private static void runTestsInAotMode(Class<?> testClass, String expectedReason) {
try {
System.setProperty(AotDetector.AOT_ENABLED, "true");

EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(testClass))
.execute()
.allEvents()
.assertThatEvents().haveExactly(1,
event(container(testClass.getSimpleName()), skippedWithReason(expectedReason)));
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
}
}


@DisabledInAotMode
static class DefaultReasonTestCase {

@Test
void test() {
}
}

@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
static class CustomReasonTestCase {

@Test
void test() {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @see MockitoSpyBeanAndCircularDependenciesWithLazyResolutionProxyIntegrationTests
*/
@SpringJUnitConfig
@DisabledInAotMode // Circular dependencies cannot be resolved in AOT mode unless a @Lazy resolution proxy is used.
@DisabledInAotMode("Circular dependencies cannot be resolved in AOT mode unless a @Lazy resolution proxy is used")
class MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests {

@MockitoSpyBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @see MockitoBeanAndContextHierarchyParentIntegrationTests
*/
@ContextHierarchy(@ContextConfiguration)
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
public class MockitoSpyBeanAndContextHierarchyChildIntegrationTests extends
MockitoBeanAndContextHierarchyParentIntegrationTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @since 4.3
*/
@ExtendWith(SpringExtension.class)
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class ContextHierarchyInterfaceTests implements ContextHierarchyTestInterface {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @since 4.1
*/
@TestPropertySource("explicit.properties")
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
@DisabledInAotMode
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
public class ExplicitPropertiesFileInClasspathTestPropertySourceTests extends AbstractExplicitPropertiesFileTests {
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
* @since 5.2
*/
@DisplayName("Explicit properties file in @TestPropertySource")
// Since Spring test's AOT processing support does not invoke test lifecycle methods such
// as @BeforeAll/@AfterAll, this test class simply is not supported for AOT processing.
@DisabledInAotMode
@DisabledInAotMode("Spring test's AOT processing support does not invoke lifecycle methods such as @BeforeAll/@AfterAll")
class ExplicitPropertiesFileTestPropertySourceTests {

static final String CURRENT_TEST_PACKAGE = "current.test.package";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
* @author Sam Brannen
* @since 4.1
*/
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
@DisabledInAotMode
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
class InheritedRelativePathPropertiesFileTestPropertySourceTests extends
ExplicitPropertiesFileInClasspathTestPropertySourceTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
* @since 4.1
*/
@TestPropertySource(properties = { "explicit = inlined", "extended = inlined1", "extended = inlined2" })
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
@DisabledInAotMode
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
class MergedPropertiesFilesOverriddenByInlinedPropertiesTestPropertySourceTests extends
MergedPropertiesFilesTestPropertySourceTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
* @since 4.1
*/
@TestPropertySource("extended.properties")
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
@DisabledInAotMode
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
class MergedPropertiesFilesTestPropertySourceTests extends
ExplicitPropertiesFileInClasspathTestPropertySourceTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
* @author Sam Brannen
* @since 4.1
*/
// Since ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode, this class must be also.
@DisabledInAotMode
@DisabledInAotMode("Because ExplicitPropertiesFileTestPropertySourceTests is disabled in AOT mode")
class SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests extends
ExplicitPropertiesFileInClasspathTestPropertySourceTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author Dave Syer
*/
@SpringJUnitConfig
@DisabledInAotMode // SpEL is not supported in AOT
@DisabledInAotMode("SpEL is not supported in AOT")
class ExpressionUsageTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -31,7 +31,7 @@
*/
@ExtendWith(SpringExtension.class)
@MetaMetaContextHierarchyConfig
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class MetaHierarchyLevelOneTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -35,7 +35,7 @@
*/
@ContextConfiguration
@ActiveProfiles("prod")
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class MetaHierarchyLevelTwoTests extends MetaHierarchyLevelOneTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -41,7 +41,7 @@
@ContextConfiguration(name = "parent", classes = ClassHierarchyWithMergedConfigLevelOneTests.AppConfig.class),//
@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelOneTests.UserConfig.class) //
})
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class ClassHierarchyWithMergedConfigLevelOneTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -35,7 +35,7 @@
*/
@ExtendWith(SpringExtension.class)
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithMergedConfigLevelTwoTests.OrderConfig.class))
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class ClassHierarchyWithMergedConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -35,7 +35,7 @@
*/
@ExtendWith(SpringExtension.class)
@ContextHierarchy(@ContextConfiguration(name = "child", classes = ClassHierarchyWithOverriddenConfigLevelTwoTests.TestUserConfig.class, inheritLocations = false))
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class ClassHierarchyWithOverriddenConfigLevelTwoTests extends ClassHierarchyWithMergedConfigLevelOneTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -52,7 +52,7 @@
@ContextConfiguration(classes = DirtiesContextWithContextHierarchyTests.ChildConfig.class)
})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class DirtiesContextWithContextHierarchyTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -36,7 +36,7 @@
*/
@ExtendWith(SpringExtension.class)
@ContextHierarchy(@ContextConfiguration)
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class SingleTestClassWithSingleLevelContextHierarchyTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -38,7 +38,7 @@
@ContextHierarchy({
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests.ParentConfig.class),
@ContextConfiguration("SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests-ChildConfig.xml") })
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
class SingleTestClassWithTwoLevelContextHierarchyAndMixedConfigTypesTests {

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -38,7 +38,7 @@
@ContextHierarchy({
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ParentConfig.class),
@ContextConfiguration(classes = SingleTestClassWithTwoLevelContextHierarchyTests.ChildConfig.class) })
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
@DisabledInAotMode("@ContextHierarchy is not supported in AOT")
public class SingleTestClassWithTwoLevelContextHierarchyTests {

@Configuration
Expand Down
Loading

0 comments on commit 86784b6

Please sign in to comment.