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 6b7f11b98c..1e0d69fc0a 100644 --- a/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java +++ b/spring-context/src/test/java/org/springframework/jmx/AbstractMBeanServerTests.java @@ -16,12 +16,17 @@ package org.springframework.jmx; +import java.net.BindException; + import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; +import org.opentest4j.TestAbortedException; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.ConfigurableApplicationContext; @@ -52,6 +57,16 @@ 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; + }; + protected MBeanServer server;