Browse Source

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
pull/28834/head
Sam Brannen 3 years ago
parent
commit
f5503298fb
  1. 5
      spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

5
spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

@ -16,6 +16,7 @@ @@ -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 abstract class AbstractTestContextBootstrapper implements TestContextBoot @@ -200,7 +201,9 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
* @see SpringFactoriesLoader#load(Class, FailureHandler)
*/
protected List<TestExecutionListener> 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 +

Loading…
Cancel
Save