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:
parent
7532876efc
commit
eacb6b13f3
|
|
@ -34,7 +34,6 @@ import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility design to run and an {@link ApplicationContext} and provide AssertJ style
|
* 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);
|
consumer.accept(context);
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
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 Madhura Bhave
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public final class TestPropertyValues {
|
public final class TestPropertyValues {
|
||||||
|
|
@ -128,11 +129,9 @@ public final class TestPropertyValues {
|
||||||
try (SystemPropertiesHandler handler = new SystemPropertiesHandler()) {
|
try (SystemPropertiesHandler handler = new SystemPropertiesHandler()) {
|
||||||
return call.call();
|
return call.call();
|
||||||
}
|
}
|
||||||
catch (RuntimeException ex) {
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
catch (Exception 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.test.context.runner;
|
package org.springframework.boot.test.context.runner;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
@ -161,8 +162,21 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void thrownRuleWorksWithCheckedException() {
|
||||||
|
get().run((context) -> {
|
||||||
|
this.thrown.expect(IOException.class);
|
||||||
|
this.thrown.expectMessage("Expected message");
|
||||||
|
throwCheckedException("Expected message");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract T get();
|
protected abstract T get();
|
||||||
|
|
||||||
|
private static void throwCheckedException(String message) throws IOException {
|
||||||
|
throw new IOException(message);
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class FailingConfig {
|
static class FailingConfig {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue