Skip to content

Commit

Permalink
Handle missing dependencies for optional TestExecutionListener again
Browse files Browse the repository at this point in the history
Commit d1b65f6 introduced a regression regarding the handling of
missing dependencies for optional (typically default)
TestExecutionListeners.

Prior to d1b65f6 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
  • Loading branch information
sbrannen committed Jul 15, 2022
1 parent 9962aa0 commit f550329
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,7 +201,9 @@ public final List<TestExecutionListener> getTestExecutionListeners() {
* @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 +
Expand Down

0 comments on commit f550329

Please sign in to comment.