|
| 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 | +} |
0 commit comments