Skip to content

Commit

Permalink
Abort MBeanServer tests if BindException encountered
Browse files Browse the repository at this point in the history
This commit builds on the previous commit but covers exceptions thrown
by @beforeeach and @AfterEach methods as well as @test methods.
  • Loading branch information
sbrannen committed Sep 24, 2019
1 parent 435cc67 commit b9013ad
Showing 1 changed file with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

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;
Expand Down Expand Up @@ -58,14 +60,7 @@
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;

Expand Down Expand Up @@ -127,4 +122,36 @@ protected void assertIsNotRegistered(String message, ObjectName objectName) {
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;
}

}

}

0 comments on commit b9013ad

Please sign in to comment.