From b9013ada9ffcd400165c7701da46ba550c9660a0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 24 Sep 2019 19:20:39 +0200 Subject: [PATCH] Abort MBeanServer tests if BindException encountered This commit builds on the previous commit but covers exceptions thrown by @BeforeEach and @AfterEach methods as well as @Test methods. --- .../jmx/AbstractMBeanServerTests.java | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java b/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java index 1e0d69fc0a..a9f7604815 100644 --- a/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java @@ -24,6 +24,8 @@ import javax.management.ObjectName; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import org.opentest4j.TestAbortedException; @@ -58,14 +60,7 @@ import static org.assertj.core.api.Assertions.assertThat; public abstract class AbstractMBeanServerTests { @RegisterExtension - TestExecutionExceptionHandler bindExceptionHandler = (context, throwable) -> { - // Abort test? - if (throwable instanceof BindException) { - throw new TestAbortedException("Failed to bind to MBeanServer", throwable); - } - // Else rethrow to conform to the contract of TestExecutionExceptionHandler - throw throwable; - }; + BindExceptionHandler bindExceptionHandler = new BindExceptionHandler(); protected MBeanServer server; @@ -127,4 +122,36 @@ public abstract class AbstractMBeanServerTests { assertThat(getServer().isRegistered(objectName)).as(message).isFalse(); } + + private static class BindExceptionHandler implements TestExecutionExceptionHandler, LifecycleMethodExecutionExceptionHandler { + + @Override + public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { + handleBindException(throwable); + } + + @Override + public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) + throws Throwable { + handleBindException(throwable); + } + + @Override + public void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) + throws Throwable { + handleBindException(throwable); + } + + private void handleBindException(Throwable throwable) throws Throwable { + // Abort test? + if (throwable instanceof BindException) { + throw new TestAbortedException("Failed to bind to MBeanServer", throwable); + } + // Else rethrow to conform to the contracts of TestExecutionExceptionHandler and LifecycleMethodExecutionExceptionHandler + throw throwable; + } + + } + } +