Skip to content

Commit

Permalink
ARQ-2231 The JUnit 5 container does not work with manual mode tests (#…
Browse files Browse the repository at this point in the history
…546)

* Run formatter on ArquillianExtension.

* Cleanup redundant modifiers in LifecycleMethodExecutor.

* ARQ-2231 Instead of implementing org.junit.jupiter.api.extension.BeforeEachCallback.beforeEach handle events within the interceptor
  • Loading branch information
rhusar authored Jun 28, 2024
1 parent b220408 commit 5e8c2de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 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 @@ -88,48 +70,68 @@ public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocatio
} else {
interceptInvocation(invocationContext, extensionContext);
getContextStore(extensionContext).getResult(extensionContext.getUniqueId())
.ifPresent(ExceptionUtils::throwAsUncheckedException);
.ifPresent(ExceptionUtils::throwAsUncheckedException);
}
}

@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
} else {
invocation.skip();
}
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();
}
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
invocation.proceed();
} else {
invocation.skip();
}
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();
}
}

@Override
public void interceptBeforeAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
invocation.skip();
} else {
invocation.proceed();
}
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
invocation.skip();
} else {
invocation.proceed();
}
}

@Override
public void interceptAfterAllMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
invocation.skip();
} else {
invocation.proceed();
}
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
invocation.skip();
} else {
invocation.proceed();
}
}

@Override
Expand Down Expand Up @@ -172,7 +174,7 @@ private void populateResults(TestResult result, ExtensionContext context) {
ContextStore contextStore = getContextStore(context);
if (result.getThrowable() instanceof IdentifiedTestException) {
((IdentifiedTestException) result.getThrowable()).getCollectedExceptions()
.forEach(contextStore::storeResult);
.forEach(contextStore::storeResult);
} else {
contextStore.storeResult(context.getUniqueId(), result.getThrowable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
* @version $Revision: $
*/
public interface LifecycleMethodExecutor {
public static final LifecycleMethodExecutor NO_OP = new LifecycleMethodExecutor() {
public void invoke() throws Throwable {
}
LifecycleMethodExecutor NO_OP = () -> {
};

void invoke() throws Throwable;
}
}

0 comments on commit 5e8c2de

Please sign in to comment.