Skip to content

Commit

Permalink
[576] Add back the afterAll and beforeEach callbacks. The TestRunnerA…
Browse files Browse the repository at this point in the history
…daptor.before() and TestRunnerAdaptor.after() need to happen in the beforeEach and afterEach listeners.
  • Loading branch information
jamezp committed Jul 2, 2024
1 parent 36c0a77 commit 76cefee
Showing 1 changed file with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
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 @@ -21,7 +23,7 @@
import static org.jboss.arquillian.junit5.ContextStore.getContextStore;
import static org.jboss.arquillian.junit5.JUnitJupiterTestClassLifecycleManager.getManager;

public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, InvocationInterceptor, TestExecutionExceptionHandler {
public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, 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 @@ -42,6 +44,22 @@ 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 @@ -76,42 +94,22 @@ 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)) {
// 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();
}
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
} else {
invocation.skip();
}
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
getManager(extensionContext).getAdaptor().after(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
invocation::proceed);
} else {
getManager(extensionContext).getAdaptor().after(
extensionContext.getRequiredTestInstance(),
extensionContext.getRequiredTestMethod(),
LifecycleMethodExecutor.NO_OP);

invocation.skip();
}
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
} else {
invocation.skip();
}
}

@Override
Expand Down

0 comments on commit 76cefee

Please sign in to comment.