Fix compatibility with JUnit's ExpectedException
This commit ensures that an exception that is thrown as part of the `ContextConsumer` callback is thrown as is. Closes gh-9878
This commit is contained in:
@@ -34,7 +34,6 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Utility design to run and an {@link ApplicationContext} and provide AssertJ style
|
||||
@@ -293,7 +292,19 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
||||
consumer.accept(context);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
ReflectionUtils.rethrowRuntimeException(ex);
|
||||
AnyThrow.throwUnchecked(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AnyThrow {
|
||||
|
||||
static void throwUnchecked(Throwable e) {
|
||||
AnyThrow.throwAny(e);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends Throwable> void throwAny(Throwable e) throws E {
|
||||
throw (E) e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public final class TestPropertyValues {
|
||||
@@ -128,11 +129,9 @@ public final class TestPropertyValues {
|
||||
try (SystemPropertiesHandler handler = new SystemPropertiesHandler()) {
|
||||
return call.call();
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
AnyThrow.throwUnchecked(ex);
|
||||
return null; // never reached
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,4 +310,16 @@ public final class TestPropertyValues {
|
||||
|
||||
}
|
||||
|
||||
private static class AnyThrow {
|
||||
|
||||
static void throwUnchecked(Throwable e) {
|
||||
AnyThrow.throwAny(e);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends Throwable> void throwAny(Throwable e) throws E {
|
||||
throw (E) e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user