Introduce @⁠DisabledInAotMode in the TestContext framework

This commit introduces @⁠DisabledInAotMode in the TestContext
framework to support the following use cases.

- Disabling AOT build-time processing of a test ApplicationContext --
  applicable to any testing framework (JUnit 4, JUnit Jupiter, etc.).

- Disabling an entire test class or a single test method at run time
  when the test suite is run with AOT optimizations enabled -- only
  applicable to JUnit Jupiter based tests.

Closes gh-30834
This commit is contained in:
Sam Brannen
2023-10-15 17:52:37 +02:00
parent 8e5f39b439
commit 39a282e463
10 changed files with 384 additions and 40 deletions

View File

@@ -72,7 +72,13 @@ abstract class AbstractAotTests {
"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/BasicTestConfiguration__TestContext005_BeanDefinitions.java"
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext005_BeanDefinitions.java",
// DisabledInAotRuntimeMethodLevelTests
"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"
};
Stream<Class<?>> scan() {

View File

@@ -45,6 +45,9 @@ import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterShar
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotProcessingTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeClassLevelTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeMethodLevelTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
@@ -98,15 +101,20 @@ class AotIntegrationTests extends AbstractAotTests {
// .printFiles(System.out)
.compile(compiled ->
// AOT RUN-TIME: EXECUTION
runTestsInAotMode(6, List.of(
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class, // NestedTests get executed automatically
runTestsInAotMode(7, 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
// 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.
BasicSpringJupiterImportedConfigTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class)));
/* 1 */ BasicSpringJupiterImportedConfigTests.class,
/* 1 */ BasicSpringTestNGTests.class,
/* 1 */ BasicSpringVintageTests.class,
/* 0 */ DisabledInAotProcessingTests.class,
/* 0 */ DisabledInAotRuntimeClassLevelTests.class,
/* 1 */ DisabledInAotRuntimeMethodLevelTests.class)));
}
@Disabled("Uncomment to run all Spring integration tests in `spring-test`")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -34,6 +34,9 @@ import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterShar
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotProcessingTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeClassLevelTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeMethodLevelTests;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -56,7 +59,10 @@ class TestAotProcessorTests extends AbstractAotTests {
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class
BasicSpringVintageTests.class,
DisabledInAotProcessingTests.class,
DisabledInAotRuntimeClassLevelTests.class,
DisabledInAotRuntimeMethodLevelTests.class
).forEach(testClass -> copy(testClass, classpathRoot));
Set<Path> classpathRoots = Set.of(classpathRoot);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -23,6 +23,9 @@ import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterShar
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotProcessingTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeClassLevelTests;
import org.springframework.test.context.aot.samples.basic.DisabledInAotRuntimeMethodLevelTests;
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,17 +45,26 @@ class TestClassScannerTests extends AbstractAotTests {
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class,
BasicSpringTestNGTests.class
DisabledInAotProcessingTests.class,
DisabledInAotRuntimeClassLevelTests.class,
DisabledInAotRuntimeMethodLevelTests.class
);
}
@Test
void scanTestSuitesForJupiter() {
assertThat(scan("org.springframework.test.context.aot.samples.suites.jupiter"))
.containsExactlyInAnyOrder(BasicSpringJupiterImportedConfigTests.class,
BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class);
.containsExactlyInAnyOrder(
BasicSpringJupiterImportedConfigTests.class,
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
DisabledInAotProcessingTests.class,
DisabledInAotRuntimeClassLevelTests.class,
DisabledInAotRuntimeMethodLevelTests.class
);
}
@Test
@@ -76,7 +88,10 @@ class TestClassScannerTests extends AbstractAotTests {
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringVintageTests.class,
BasicSpringTestNGTests.class
BasicSpringTestNGTests.class,
DisabledInAotProcessingTests.class,
DisabledInAotRuntimeClassLevelTests.class,
DisabledInAotRuntimeMethodLevelTests.class
);
}
@@ -88,7 +103,10 @@ class TestClassScannerTests extends AbstractAotTests {
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringVintageTests.class
BasicSpringVintageTests.class,
DisabledInAotProcessingTests.class,
DisabledInAotRuntimeClassLevelTests.class,
DisabledInAotRuntimeMethodLevelTests.class
);
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2023 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.Test;
import org.springframework.aot.AotDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.aot.TestContextAotGenerator;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@code @DisabledInAotMode} test class which verifies that the application context
* for the test class is skipped during AOT processing.
*
* @author Sam Brannen
* @since 6.1
*/
@SpringJUnitConfig
@DisabledInAotMode
public class DisabledInAotProcessingTests {
@Test
void disabledInAotMode(@Autowired String enigma) {
assertThat(AotDetector.useGeneratedArtifacts()).as("Should be disabled in AOT mode").isFalse();
assertThat(enigma).isEqualTo("puzzle");
}
@Configuration
static class Config {
@Bean
String enigma() {
return "puzzle";
}
@Bean
static BeanFactoryPostProcessor bfppBrokenDuringAotProcessing() {
boolean runningDuringAotProcessing = StackWalker.getInstance().walk(stream ->
stream.anyMatch(stackFrame -> stackFrame.getClassName().equals(TestContextAotGenerator.class.getName())));
return beanFactory -> Assert.state(!runningDuringAotProcessing, "Should not be used during AOT processing");
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2023 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.Test;
import org.springframework.aot.AotDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.aot.samples.common.DefaultMessageService;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@code @DisabledInAotMode} class-level tests.
*
* <p>This test class differs from {@link DisabledInAotProcessingTests} whose
* {@code ApplicationContext} will simply fail during AOT processing. Whereas,
* the {@code ApplicationContext} for this test class can be properly processed
* for AOT optimizations, but we want to ensure that we can also disable such a
* test class in AOT mode if desired.
*
* @author Sam Brannen
* @since 6.1
* @see DisabledInAotRuntimeMethodLevelTests
* @see DisabledInAotProcessingTests
*/
@SpringJUnitConfig
@TestPropertySource(properties = "disabledInAotMode = class-level")
@DisabledInAotMode
public class DisabledInAotRuntimeClassLevelTests {
@Test
void test(@Autowired ApplicationContext context, @Autowired MessageService messageService,
@Value("${disabledInAotMode}") String disabledInAotMode) {
assertThat(AotDetector.useGeneratedArtifacts()).as("Should be disabled in AOT mode").isFalse();
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
assertThat(disabledInAotMode).isEqualTo("class-level");
}
@Configuration(proxyBeanMethods = false)
static class BasicTestConfiguration {
@Bean
MessageService defaultMessageService() {
return new DefaultMessageService();
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2002-2023 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.Test;
import org.springframework.aot.AotDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.aot.samples.common.DefaultMessageService;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@code @DisabledInAotMode} method-level tests.
*
* <p>The {@code ApplicationContext} will still be processed for AOT optimizations
* and used for the {@link #test} method (in standard JVM mode and in AOT mode),
* but the {@link #disabledInAotMode()} method will not be executed in AOT mode.
*
* @author Sam Brannen
* @since 6.1
* @see DisabledInAotRuntimeClassLevelTests
* @see DisabledInAotProcessingTests
*/
@SpringJUnitConfig
@TestPropertySource(properties = "disabledInAotMode = method-level")
public class DisabledInAotRuntimeMethodLevelTests {
@Test
void test(@Autowired ApplicationContext context, @Autowired MessageService messageService,
@Value("${disabledInAotMode}") String disabledInAotMode) {
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
assertThat(disabledInAotMode).isEqualTo("method-level");
}
@Test
@DisabledInAotMode
void disabledInAotMode() {
assertThat(AotDetector.useGeneratedArtifacts()).as("Should be disabled in AOT mode").isFalse();
}
@Configuration(proxyBeanMethods = false)
static class BasicTestConfiguration {
@Bean
MessageService defaultMessageService() {
return new DefaultMessageService();
}
}
}