From f5503298fb00d0dde053ec3bb4cd9f91c41313a2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 15 Jul 2022 13:50:28 +0200 Subject: [PATCH] Handle missing dependencies for optional TestExecutionListener again Commit d1b65f6d3e introduced a regression regarding the handling of missing dependencies for optional (typically default) TestExecutionListeners. Prior to d1b65f6d3e a TestExecutionListener was instantiated using java.lang.Class.newInstance() which never throws an InvocationTargetException. With the switch to the new SpringFactoriesLoader APIs, a TestExecutionListener is now instantiated using java.lang.reflect.Constructor.newInstance(Object...) which can throw an InvocationTargetException. This commit addresses the regression by unwrapping the target exception in an InvocationTargetException. See gh-28666 Closes gh-28828 --- .../context/support/AbstractTestContextBootstrapper.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java index 157bb6ba267d..6e8a9c713d6f 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java @@ -16,6 +16,7 @@ package org.springframework.test.context.support; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -200,7 +201,9 @@ public final List getTestExecutionListeners() { * @see SpringFactoriesLoader#load(Class, FailureHandler) */ protected List getDefaultTestExecutionListeners() { - FailureHandler failureHandler = (factoryType, factoryImplementationName, ex) -> { + FailureHandler failureHandler = (factoryType, factoryImplementationName, failure) -> { + Throwable ex = (failure instanceof InvocationTargetException ite ? + ite.getTargetException() : failure); if (ex instanceof LinkageError || ex instanceof ClassNotFoundException) { if (logger.isDebugEnabled()) { logger.debug("Could not load default TestExecutionListener [" + factoryImplementationName +