Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2025-10-22 17:09:38 +02:00
22 changed files with 1865 additions and 48 deletions
@@ -93,9 +93,17 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
* a corresponding bean override instance.
*/
private static void injectFields(TestContext testContext) {
List<BeanOverrideHandler> handlers = BeanOverrideHandler.forTestClass(testContext.getTestClass());
Object testInstance = testContext.getTestInstance();
// Since JUnit Jupiter 5.12, if the SpringExtension is used with Jupiter's
// ExtensionContextScope.TEST_METHOD mode, the value returned from
// testContext.getTestClass() may refer to the declaring class of the test
// method which is about to be invoked (which may be in a @Nested class
// within the class for the test instance). Thus, we use the class for the
// test instance as the "test class".
Class<?> testClass = testInstance.getClass();
List<BeanOverrideHandler> handlers = BeanOverrideHandler.forTestClass(testClass);
if (!handlers.isEmpty()) {
Object testInstance = testContext.getTestInstance();
ApplicationContext applicationContext = testContext.getApplicationContext();
Assert.state(applicationContext.containsBean(BeanOverrideRegistry.BEAN_NAME), () -> """
@@ -302,18 +302,19 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter();
Class<?> parameterType = parameter.getType();
Executable executable = parameter.getDeclaringExecutable();
PropertyProvider junitPropertyProvider = propertyName ->
extensionContext.getConfigurationParameter(propertyName).orElse(null);
return (TestConstructorUtils.isAutowirableConstructor(executable, junitPropertyProvider) ||
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
supportsApplicationEvents(parameterContext) ||
ApplicationContext.class.isAssignableFrom(parameterType) ||
supportsApplicationEvents(parameterType, executable) ||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
private boolean supportsApplicationEvents(ParameterContext parameterContext) {
if (ApplicationEvents.class.isAssignableFrom(parameterContext.getParameter().getType())) {
Assert.isTrue(parameterContext.getDeclaringExecutable() instanceof Method,
private boolean supportsApplicationEvents(Class<?> parameterType, Executable executable) {
if (ApplicationEvents.class.isAssignableFrom(parameterType)) {
Assert.isTrue(executable instanceof Method,
"ApplicationEvents can only be injected into test and lifecycle methods");
return true;
}