3
3
import io .cucumber .core .backend .Glue ;
4
4
import io .cucumber .core .backend .HookDefinition ;
5
5
import io .cucumber .core .backend .StubStepDefinition ;
6
+ import io .cucumber .core .backend .TestCaseState ;
6
7
import io .cucumber .core .eventbus .EventBus ;
7
8
import io .cucumber .core .feature .TestFeatureParser ;
8
9
import io .cucumber .core .gherkin .Feature ;
9
10
import io .cucumber .core .gherkin .Pickle ;
10
11
import io .cucumber .core .options .RuntimeOptions ;
11
12
import io .cucumber .core .runtime .TimeServiceEventBus ;
12
13
import org .junit .jupiter .api .Test ;
13
- import org .mockito .ArgumentMatchers ;
14
- import org .mockito .InOrder ;
15
14
16
15
import java .net .URI ;
17
16
import java .time .Clock ;
18
17
import java .util .ArrayList ;
19
18
import java .util .List ;
20
19
import java .util .UUID ;
21
20
22
- import static org .mockito .Mockito .inOrder ;
23
- import static org .mockito .Mockito .mock ;
24
- import static org .mockito .Mockito .when ;
21
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
25
22
26
23
class HookOrderTest {
27
24
@@ -34,10 +31,12 @@ class HookOrderTest {
34
31
" Scenario: Test scenario\n " +
35
32
" Given I have 4 cukes in my belly\n " );
36
33
private final Pickle pickle = feature .getPickles ().get (0 );
34
+ private final List <HookDefinition > listener = new ArrayList <>();
37
35
38
36
@ Test
39
37
void before_hooks_execute_in_order () {
40
- final List <HookDefinition > hooks = mockHooks (3 , Integer .MAX_VALUE , 1 , -1 , 0 , 10000 , Integer .MIN_VALUE );
38
+ final List <HookDefinition > hooks = mockHooks (listener , 3 , Integer .MAX_VALUE , 1 , -1 , 0 , 10000 ,
39
+ Integer .MIN_VALUE );
41
40
42
41
TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
43
42
@ Override
@@ -52,31 +51,37 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
52
51
53
52
runnerSupplier .get ().runPickle (pickle );
54
53
55
- InOrder inOrder = inOrder (hooks .toArray ());
56
- inOrder .verify (hooks .get (6 )).execute (ArgumentMatchers .any ());
57
- inOrder .verify (hooks .get (3 )).execute (ArgumentMatchers .any ());
58
- inOrder .verify (hooks .get (4 )).execute (ArgumentMatchers .any ());
59
- inOrder .verify (hooks .get (2 )).execute (ArgumentMatchers .any ());
60
- inOrder .verify (hooks .get (0 )).execute (ArgumentMatchers .any ());
61
- inOrder .verify (hooks .get (5 )).execute (ArgumentMatchers .any ());
62
- inOrder .verify (hooks .get (1 )).execute (ArgumentMatchers .any ());
54
+ verifyHookDefinitionExecutedInOrder ();
63
55
}
64
56
65
- private List <HookDefinition > mockHooks (int ... ordering ) {
57
+ private void verifyHookDefinitionExecutedInOrder () {
58
+ long previousOrder = Long .MIN_VALUE ;
59
+ for (HookDefinition hd : listener ) {
60
+ assertTrue (hd .getOrder () >= previousOrder );
61
+ previousOrder = hd .getOrder ();
62
+ }
63
+ }
64
+
65
+ private void verifyHookDefinitionExecutedInReverseOrder () {
66
+ long previousOrder = Long .MAX_VALUE ;
67
+ for (HookDefinition hd : listener ) {
68
+ assertTrue (hd .getOrder () <= previousOrder );
69
+ previousOrder = hd .getOrder ();
70
+ }
71
+ }
72
+
73
+ private List <HookDefinition > mockHooks (List <HookDefinition > listener , int ... ordering ) {
66
74
List <HookDefinition > hooks = new ArrayList <>();
67
75
for (int order : ordering ) {
68
- HookDefinition hook = mock (HookDefinition .class , "Mock number " + order );
69
- when (hook .getOrder ()).thenReturn (order );
70
- when (hook .getTagExpression ()).thenReturn ("" );
71
- when (hook .getLocation ()).thenReturn ("Mock location" );
72
- hooks .add (hook );
76
+ hooks .add (new MockHookDefinition (order , listener ));
73
77
}
74
78
return hooks ;
75
79
}
76
80
77
81
@ Test
78
82
void before_step_hooks_execute_in_order () {
79
- final List <HookDefinition > hooks = mockHooks (3 , Integer .MAX_VALUE , 1 , -1 , 0 , 10000 , Integer .MIN_VALUE );
83
+ final List <HookDefinition > hooks = mockHooks (listener , 3 , Integer .MAX_VALUE , 1 , -1 , 0 , 10000 ,
84
+ Integer .MIN_VALUE );
80
85
81
86
TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
82
87
@ Override
@@ -91,19 +96,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
91
96
92
97
runnerSupplier .get ().runPickle (pickle );
93
98
94
- InOrder inOrder = inOrder (hooks .toArray ());
95
- inOrder .verify (hooks .get (6 )).execute (ArgumentMatchers .any ());
96
- inOrder .verify (hooks .get (3 )).execute (ArgumentMatchers .any ());
97
- inOrder .verify (hooks .get (4 )).execute (ArgumentMatchers .any ());
98
- inOrder .verify (hooks .get (2 )).execute (ArgumentMatchers .any ());
99
- inOrder .verify (hooks .get (0 )).execute (ArgumentMatchers .any ());
100
- inOrder .verify (hooks .get (5 )).execute (ArgumentMatchers .any ());
101
- inOrder .verify (hooks .get (1 )).execute (ArgumentMatchers .any ());
99
+ verifyHookDefinitionExecutedInOrder ();
102
100
}
103
101
104
102
@ Test
105
103
void after_hooks_execute_in_reverse_order () {
106
- final List <HookDefinition > hooks = mockHooks (Integer .MIN_VALUE , 2 , Integer .MAX_VALUE , 4 , -1 , 0 , 10000 );
104
+ final List <HookDefinition > hooks = mockHooks (listener , Integer .MIN_VALUE , 2 , Integer .MAX_VALUE , 4 , -1 , 0 ,
105
+ 10000 );
107
106
108
107
TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
109
108
@ Override
@@ -118,19 +117,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
118
117
119
118
runnerSupplier .get ().runPickle (pickle );
120
119
121
- InOrder inOrder = inOrder (hooks .toArray ());
122
- inOrder .verify (hooks .get (2 )).execute (ArgumentMatchers .any ());
123
- inOrder .verify (hooks .get (6 )).execute (ArgumentMatchers .any ());
124
- inOrder .verify (hooks .get (3 )).execute (ArgumentMatchers .any ());
125
- inOrder .verify (hooks .get (1 )).execute (ArgumentMatchers .any ());
126
- inOrder .verify (hooks .get (5 )).execute (ArgumentMatchers .any ());
127
- inOrder .verify (hooks .get (4 )).execute (ArgumentMatchers .any ());
128
- inOrder .verify (hooks .get (0 )).execute (ArgumentMatchers .any ());
120
+ verifyHookDefinitionExecutedInReverseOrder ();
129
121
}
130
122
131
123
@ Test
132
124
void after_step_hooks_execute_in_reverse_order () {
133
- final List <HookDefinition > hooks = mockHooks (Integer .MIN_VALUE , 2 , Integer .MAX_VALUE , 4 , -1 , 0 , 10000 );
125
+ final List <HookDefinition > hooks = mockHooks (listener , Integer .MIN_VALUE , 2 , Integer .MAX_VALUE , 4 , -1 , 0 ,
126
+ 10000 );
134
127
135
128
TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
136
129
@ Override
@@ -145,20 +138,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
145
138
146
139
runnerSupplier .get ().runPickle (pickle );
147
140
148
- InOrder inOrder = inOrder (hooks .toArray ());
149
- inOrder .verify (hooks .get (2 )).execute (ArgumentMatchers .any ());
150
- inOrder .verify (hooks .get (6 )).execute (ArgumentMatchers .any ());
151
- inOrder .verify (hooks .get (3 )).execute (ArgumentMatchers .any ());
152
- inOrder .verify (hooks .get (1 )).execute (ArgumentMatchers .any ());
153
- inOrder .verify (hooks .get (5 )).execute (ArgumentMatchers .any ());
154
- inOrder .verify (hooks .get (4 )).execute (ArgumentMatchers .any ());
155
- inOrder .verify (hooks .get (0 )).execute (ArgumentMatchers .any ());
141
+ verifyHookDefinitionExecutedInReverseOrder ();
156
142
}
157
143
158
144
@ Test
159
145
void hooks_order_across_many_backends () {
160
- final List <HookDefinition > backend1Hooks = mockHooks (3 , Integer .MAX_VALUE , 1 );
161
- final List <HookDefinition > backend2Hooks = mockHooks (2 , Integer .MAX_VALUE , 4 );
146
+ final List <HookDefinition > backend1Hooks = mockHooks (listener , 3 , Integer .MAX_VALUE , 1 );
147
+ final List <HookDefinition > backend2Hooks = mockHooks (listener , 2 , Integer .MAX_VALUE , 4 );
162
148
163
149
TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
164
150
@ Override
@@ -177,17 +163,41 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
177
163
178
164
runnerSupplier .get ().runPickle (pickle );
179
165
180
- List <HookDefinition > allHooks = new ArrayList <>();
181
- allHooks .addAll (backend1Hooks );
182
- allHooks .addAll (backend2Hooks );
183
-
184
- InOrder inOrder = inOrder (allHooks .toArray ());
185
- inOrder .verify (backend1Hooks .get (2 )).execute (ArgumentMatchers .any ());
186
- inOrder .verify (backend2Hooks .get (0 )).execute (ArgumentMatchers .any ());
187
- inOrder .verify (backend1Hooks .get (0 )).execute (ArgumentMatchers .any ());
188
- inOrder .verify (backend2Hooks .get (2 )).execute (ArgumentMatchers .any ());
189
- inOrder .verify (backend1Hooks .get (1 )).execute (ArgumentMatchers .any ());
190
- inOrder .verify (backend2Hooks .get (1 )).execute (ArgumentMatchers .any ());
166
+ verifyHookDefinitionExecutedInOrder ();
191
167
}
192
168
169
+ private static class MockHookDefinition implements HookDefinition {
170
+ private final int order ;
171
+ private final List <HookDefinition > listener ;
172
+
173
+ public MockHookDefinition (int order , List <HookDefinition > listener ) {
174
+ this .order = order ;
175
+ this .listener = listener ;
176
+ }
177
+
178
+ @ Override
179
+ public void execute (TestCaseState state ) {
180
+ listener .add (this );
181
+ }
182
+
183
+ @ Override
184
+ public String getTagExpression () {
185
+ return "" ;
186
+ }
187
+
188
+ @ Override
189
+ public int getOrder () {
190
+ return order ;
191
+ }
192
+
193
+ @ Override
194
+ public boolean isDefinedAt (StackTraceElement stackTraceElement ) {
195
+ return false ;
196
+ }
197
+
198
+ @ Override
199
+ public String getLocation () {
200
+ return "Mock location" ;
201
+ }
202
+ }
193
203
}
0 commit comments