10
10
import org .jboss .arquillian .test .spi .TestMethodExecutor ;
11
11
import org .jboss .arquillian .test .spi .TestResult ;
12
12
import org .junit .jupiter .api .extension .AfterAllCallback ;
13
- import org .junit .jupiter .api .extension .AfterEachCallback ;
14
13
import org .junit .jupiter .api .extension .BeforeAllCallback ;
15
- import org .junit .jupiter .api .extension .BeforeEachCallback ;
16
14
import org .junit .jupiter .api .extension .ExtensionContext ;
17
15
import org .junit .jupiter .api .extension .InvocationInterceptor ;
18
16
import org .junit .jupiter .api .extension .ReflectiveInvocationContext ;
23
21
import static org .jboss .arquillian .junit5 .ContextStore .getContextStore ;
24
22
import static org .jboss .arquillian .junit5 .JUnitJupiterTestClassLifecycleManager .getManager ;
25
23
26
- public class ArquillianExtension implements BeforeAllCallback , AfterAllCallback , BeforeEachCallback , AfterEachCallback , InvocationInterceptor , TestExecutionExceptionHandler {
24
+ public class ArquillianExtension implements BeforeAllCallback , AfterAllCallback , InvocationInterceptor , TestExecutionExceptionHandler {
27
25
public static final String RUNNING_INSIDE_ARQUILLIAN = "insideArquillian" ;
28
26
29
27
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 {
44
42
LifecycleMethodExecutor .NO_OP );
45
43
}
46
44
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
-
63
45
@ Override
64
46
public void interceptTestTemplateMethod (Invocation <Void > invocation , ReflectiveInvocationContext <Method > invocationContext , ExtensionContext extensionContext ) throws Throwable {
65
47
if (IS_INSIDE_ARQUILLIAN .test (extensionContext )) {
@@ -88,48 +70,68 @@ public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocatio
88
70
} else {
89
71
interceptInvocation (invocationContext , extensionContext );
90
72
getContextStore (extensionContext ).getResult (extensionContext .getUniqueId ())
91
- .ifPresent (ExceptionUtils ::throwAsUncheckedException );
73
+ .ifPresent (ExceptionUtils ::throwAsUncheckedException );
92
74
}
93
75
}
94
76
95
77
@ Override
96
78
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
+ }
103
97
}
104
98
105
99
@ Override
106
100
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
+ }
113
115
}
114
116
115
117
@ Override
116
118
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
+ }
123
125
}
124
126
125
127
@ Override
126
128
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
+ }
133
135
}
134
136
135
137
@ Override
@@ -172,7 +174,7 @@ private void populateResults(TestResult result, ExtensionContext context) {
172
174
ContextStore contextStore = getContextStore (context );
173
175
if (result .getThrowable () instanceof IdentifiedTestException ) {
174
176
((IdentifiedTestException ) result .getThrowable ()).getCollectedExceptions ()
175
- .forEach (contextStore ::storeResult );
177
+ .forEach (contextStore ::storeResult );
176
178
} else {
177
179
contextStore .storeResult (context .getUniqueId (), result .getThrowable ());
178
180
}
0 commit comments