Skip to content

Commit

Permalink
ARQ-2231 Instead of implementing org.junit.jupiter.api.extension.Befo…
Browse files Browse the repository at this point in the history
…reEachCallback.beforeEach handle events within the interceptor
  • Loading branch information
rhusar committed Apr 3, 2024
1 parent 6ee1dd4 commit abbb660
Showing 1 changed file with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.jboss.arquillian.test.spi.TestMethodExecutor;
import org.jboss.arquillian.test.spi.TestResult;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
Expand All @@ -23,7 +21,7 @@
import static org.jboss.arquillian.junit5.ContextStore.getContextStore;
import static org.jboss.arquillian.junit5.JUnitJupiterTestClassLifecycleManager.getManager;

public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, InvocationInterceptor, TestExecutionExceptionHandler {
public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, InvocationInterceptor, TestExecutionExceptionHandler {
public static final String RUNNING_INSIDE_ARQUILLIAN = "insideArquillian";

private static final String CHAIN_EXCEPTION_MESSAGE_PREFIX = "Chain of InvocationInterceptors never called invocation";
Expand All @@ -44,22 +42,6 @@ public void afterAll(ExtensionContext context) throws Exception {
LifecycleMethodExecutor.NO_OP);
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
getManager(context).getAdaptor().before(
context.getRequiredTestInstance(),
context.getRequiredTestMethod(),
LifecycleMethodExecutor.NO_OP);
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
getManager(context).getAdaptor().after(
context.getRequiredTestInstance(),
context.getRequiredTestMethod(),
LifecycleMethodExecutor.NO_OP);
}

@Override
public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
Expand Down Expand Up @@ -95,9 +77,21 @@ public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocatio
@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
// Instead of implementing org.junit.jupiter.api.extension.BeforeEachCallback.beforeEach handle events within the interceptor
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
// Since the invocation is going to proceed, the invocation must happen within the context of SPI before()
getManager(extensionContext).getAdaptor().before(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
invocation::proceed);
} else {
// Ensure the SPI before() is called, but given that the execution is going to be skipped
getManager(extensionContext).getAdaptor().before(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
LifecycleMethodExecutor.NO_OP);

// and ensure that the contract of the org.junit.jupiter.api.extension.InvocationInterceptor will be fulfilled.
invocation.skip();
}
}
Expand All @@ -106,8 +100,16 @@ public void interceptBeforeEachMethod(Invocation<Void> invocation,
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
getManager(extensionContext).getAdaptor().after(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
invocation::proceed);
} else {
getManager(extensionContext).getAdaptor().after(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
LifecycleMethodExecutor.NO_OP);

invocation.skip();
}
}
Expand Down

0 comments on commit abbb660

Please # to comment.