33import io .cucumber .core .backend .Glue ;
44import io .cucumber .core .backend .HookDefinition ;
55import io .cucumber .core .backend .StubStepDefinition ;
6+ import io .cucumber .core .backend .TestCaseState ;
67import io .cucumber .core .eventbus .EventBus ;
78import io .cucumber .core .feature .TestFeatureParser ;
89import io .cucumber .core .gherkin .Feature ;
910import io .cucumber .core .gherkin .Pickle ;
1011import io .cucumber .core .options .RuntimeOptions ;
1112import io .cucumber .core .runtime .TimeServiceEventBus ;
1213import org .junit .jupiter .api .Test ;
13- import org .mockito .ArgumentMatchers ;
14- import org .mockito .InOrder ;
1514
1615import java .net .URI ;
1716import java .time .Clock ;
1817import java .util .ArrayList ;
1918import java .util .List ;
2019import java .util .UUID ;
2120
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 ;
2522
2623class HookOrderTest {
2724
@@ -34,10 +31,12 @@ class HookOrderTest {
3431 " Scenario: Test scenario\n " +
3532 " Given I have 4 cukes in my belly\n " );
3633 private final Pickle pickle = feature .getPickles ().get (0 );
34+ private final List <HookDefinition > listener = new ArrayList <>();
3735
3836 @ Test
3937 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 );
4140
4241 TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
4342 @ Override
@@ -52,31 +51,37 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
5251
5352 runnerSupplier .get ().runPickle (pickle );
5453
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 ();
6355 }
6456
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 ) {
6674 List <HookDefinition > hooks = new ArrayList <>();
6775 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 ));
7377 }
7478 return hooks ;
7579 }
7680
7781 @ Test
7882 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 );
8085
8186 TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
8287 @ Override
@@ -91,19 +96,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
9196
9297 runnerSupplier .get ().runPickle (pickle );
9398
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 ();
102100 }
103101
104102 @ Test
105103 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 );
107106
108107 TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
109108 @ Override
@@ -118,19 +117,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
118117
119118 runnerSupplier .get ().runPickle (pickle );
120119
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 ();
129121 }
130122
131123 @ Test
132124 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 );
134127
135128 TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
136129 @ Override
@@ -145,20 +138,13 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
145138
146139 runnerSupplier .get ().runPickle (pickle );
147140
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 ();
156142 }
157143
158144 @ Test
159145 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 );
162148
163149 TestRunnerSupplier runnerSupplier = new TestRunnerSupplier (bus , runtimeOptions ) {
164150 @ Override
@@ -177,17 +163,41 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
177163
178164 runnerSupplier .get ().runPickle (pickle );
179165
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 ();
191167 }
192168
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+ }
193203}
0 commit comments