Skip to content

Commit 5e8c2de

Browse files
authored
ARQ-2231 The JUnit 5 container does not work with manual mode tests (#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
1 parent b220408 commit 5e8c2de

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

junit5/core/src/main/java/org/jboss/arquillian/junit5/ArquillianExtension.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
import org.jboss.arquillian.test.spi.TestMethodExecutor;
1111
import org.jboss.arquillian.test.spi.TestResult;
1212
import org.junit.jupiter.api.extension.AfterAllCallback;
13-
import org.junit.jupiter.api.extension.AfterEachCallback;
1413
import org.junit.jupiter.api.extension.BeforeAllCallback;
15-
import org.junit.jupiter.api.extension.BeforeEachCallback;
1614
import org.junit.jupiter.api.extension.ExtensionContext;
1715
import org.junit.jupiter.api.extension.InvocationInterceptor;
1816
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
@@ -23,7 +21,7 @@
2321
import static org.jboss.arquillian.junit5.ContextStore.getContextStore;
2422
import static org.jboss.arquillian.junit5.JUnitJupiterTestClassLifecycleManager.getManager;
2523

26-
public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, InvocationInterceptor, TestExecutionExceptionHandler {
24+
public class ArquillianExtension implements BeforeAllCallback, AfterAllCallback, InvocationInterceptor, TestExecutionExceptionHandler {
2725
public static final String RUNNING_INSIDE_ARQUILLIAN = "insideArquillian";
2826

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

47-
@Override
48-
public void beforeEach(ExtensionContext context) throws Exception {
49-
getManager(context).getAdaptor().before(
50-
context.getRequiredTestInstance(),
51-
context.getRequiredTestMethod(),
52-
LifecycleMethodExecutor.NO_OP);
53-
}
54-
55-
@Override
56-
public void afterEach(ExtensionContext context) throws Exception {
57-
getManager(context).getAdaptor().after(
58-
context.getRequiredTestInstance(),
59-
context.getRequiredTestMethod(),
60-
LifecycleMethodExecutor.NO_OP);
61-
}
62-
6345
@Override
6446
public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
6547
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
@@ -88,48 +70,68 @@ public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocatio
8870
} else {
8971
interceptInvocation(invocationContext, extensionContext);
9072
getContextStore(extensionContext).getResult(extensionContext.getUniqueId())
91-
.ifPresent(ExceptionUtils::throwAsUncheckedException);
73+
.ifPresent(ExceptionUtils::throwAsUncheckedException);
9274
}
9375
}
9476

9577
@Override
9678
public void interceptBeforeEachMethod(Invocation<Void> invocation,
97-
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
98-
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
99-
invocation.proceed();
100-
} else {
101-
invocation.skip();
102-
}
79+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
80+
// Instead of implementing org.junit.jupiter.api.extension.BeforeEachCallback.beforeEach handle events within the interceptor
81+
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
82+
// Since the invocation is going to proceed, the invocation must happen within the context of SPI before()
83+
getManager(extensionContext).getAdaptor().before(
84+
extensionContext.getRequiredTestInstance(),
85+
extensionContext.getRequiredTestMethod(),
86+
invocation::proceed);
87+
} else {
88+
// Ensure the SPI before() is called, but given that the execution is going to be skipped
89+
getManager(extensionContext).getAdaptor().before(
90+
extensionContext.getRequiredTestInstance(),
91+
extensionContext.getRequiredTestMethod(),
92+
LifecycleMethodExecutor.NO_OP);
93+
94+
// and ensure that the contract of the org.junit.jupiter.api.extension.InvocationInterceptor will be fulfilled.
95+
invocation.skip();
96+
}
10397
}
10498

10599
@Override
106100
public void interceptAfterEachMethod(Invocation<Void> invocation,
107-
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
108-
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
109-
invocation.proceed();
110-
} else {
111-
invocation.skip();
112-
}
101+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
102+
if (IS_INSIDE_ARQUILLIAN.test(extensionContext) || isRunAsClient(extensionContext)) {
103+
getManager(extensionContext).getAdaptor().after(
104+
extensionContext.getRequiredTestInstance(),
105+
extensionContext.getRequiredTestMethod(),
106+
invocation::proceed);
107+
} else {
108+
getManager(extensionContext).getAdaptor().after(
109+
extensionContext.getRequiredTestInstance(),
110+
extensionContext.getRequiredTestMethod(),
111+
LifecycleMethodExecutor.NO_OP);
112+
113+
invocation.skip();
114+
}
113115
}
114116

115117
@Override
116118
public void interceptBeforeAllMethod(Invocation<Void> invocation,
117-
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
118-
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
119-
invocation.skip();
120-
} else {
121-
invocation.proceed();
122-
}
119+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
120+
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
121+
invocation.skip();
122+
} else {
123+
invocation.proceed();
124+
}
123125
}
124126

125127
@Override
126128
public void interceptAfterAllMethod(Invocation<Void> invocation,
127-
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
128-
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
129-
invocation.skip();
130-
} else {
131-
invocation.proceed();
132-
}
129+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
130+
if (IS_INSIDE_ARQUILLIAN.test(extensionContext)) {
131+
invocation.skip();
132+
} else {
133+
invocation.proceed();
134+
}
133135
}
134136

135137
@Override
@@ -172,7 +174,7 @@ private void populateResults(TestResult result, ExtensionContext context) {
172174
ContextStore contextStore = getContextStore(context);
173175
if (result.getThrowable() instanceof IdentifiedTestException) {
174176
((IdentifiedTestException) result.getThrowable()).getCollectedExceptions()
175-
.forEach(contextStore::storeResult);
177+
.forEach(contextStore::storeResult);
176178
} else {
177179
contextStore.storeResult(context.getUniqueId(), result.getThrowable());
178180
}

test/spi/src/main/java/org/jboss/arquillian/test/spi/LifecycleMethodExecutor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
* @version $Revision: $
2626
*/
2727
public interface LifecycleMethodExecutor {
28-
public static final LifecycleMethodExecutor NO_OP = new LifecycleMethodExecutor() {
29-
public void invoke() throws Throwable {
30-
}
28+
LifecycleMethodExecutor NO_OP = () -> {
3129
};
3230

3331
void invoke() throws Throwable;
34-
}
32+
}

0 commit comments

Comments
 (0)