4
4
import io .cucumber .core .backend .Glue ;
5
5
import io .cucumber .core .backend .HookDefinition ;
6
6
import io .cucumber .core .backend .ObjectFactory ;
7
+ import io .cucumber .core .backend .Snippet ;
8
+ import io .cucumber .core .backend .TestCaseState ;
7
9
import io .cucumber .core .eventbus .EventBus ;
8
10
import io .cucumber .core .feature .TestFeatureParser ;
9
11
import io .cucumber .core .gherkin .Feature ;
12
14
import io .cucumber .core .runtime .TimeServiceEventBus ;
13
15
import io .cucumber .core .snippets .TestSnippet ;
14
16
import org .junit .jupiter .api .Test ;
15
- import org .mockito .ArgumentMatchers ;
16
- import org .mockito .InOrder ;
17
17
18
+ import java .net .URI ;
18
19
import java .time .Clock ;
20
+ import java .util .ArrayList ;
19
21
import java .util .Collections ;
22
+ import java .util .List ;
20
23
import java .util .UUID ;
21
24
22
25
import static org .hamcrest .MatcherAssert .assertThat ;
23
26
import static org .hamcrest .Matchers .is ;
27
+ import static org .junit .jupiter .api .Assertions .assertLinesMatch ;
24
28
import static org .junit .jupiter .api .Assertions .assertThrows ;
25
- import static org .mockito .ArgumentMatchers .any ;
26
- import static org .mockito .Mockito .doAnswer ;
27
- import static org .mockito .Mockito .inOrder ;
28
- import static org .mockito .Mockito .mock ;
29
- import static org .mockito .Mockito .when ;
30
29
31
30
class HookTest {
32
31
@@ -44,43 +43,23 @@ class HookTest {
44
43
*/
45
44
@ Test
46
45
void after_hooks_execute_before_objects_are_disposed () {
47
- Backend backend = mock (Backend .class );
48
- when (backend .getSnippet ()).thenReturn (new TestSnippet ());
46
+ final List <String > eventListener = new ArrayList <>();
47
+ final HookDefinition hook = new MockHookDefinition ("" , "hook-location" , eventListener );
48
+ Backend backend = new StubBackend (hook , eventListener );
49
49
ObjectFactory objectFactory = new StubObjectFactory ();
50
- final HookDefinition hook = mock (HookDefinition .class );
51
- when (hook .getLocation ()).thenReturn ("hook-location" );
52
- when (hook .getTagExpression ()).thenReturn ("" );
53
-
54
- doAnswer (invocation -> {
55
- Glue glue = invocation .getArgument (0 );
56
- glue .addBeforeHook (hook );
57
- return null ;
58
- }).when (backend ).loadGlue (any (Glue .class ), ArgumentMatchers .anyList ());
59
-
60
50
Runner runner = new Runner (bus , Collections .singleton (backend ), objectFactory , runtimeOptions );
61
51
62
52
runner .runPickle (pickle );
63
53
64
- InOrder inOrder = inOrder (hook , backend );
65
- inOrder .verify (backend ).buildWorld ();
66
- inOrder .verify (hook ).execute (ArgumentMatchers .any ());
67
- inOrder .verify (backend ).disposeWorld ();
54
+ assertLinesMatch (eventListener , List .of ("buildWorld" , "execute" , "disposeWorld" ));
68
55
}
69
56
70
57
@ Test
71
58
void hook_throws_exception_with_name_when_tag_expression_is_invalid () {
72
- Backend backend = mock (Backend .class );
73
- when (backend .getSnippet ()).thenReturn (new TestSnippet ());
59
+ final List <String > eventListener = new ArrayList <>();
60
+ final HookDefinition hook = new MockHookDefinition ("(" , "hook-location" , eventListener );
61
+ Backend backend = new StubBackend (hook , eventListener );
74
62
ObjectFactory objectFactory = new StubObjectFactory ();
75
- final HookDefinition hook = mock (HookDefinition .class );
76
- when (hook .getLocation ()).thenReturn ("hook-location" );
77
- when (hook .getTagExpression ()).thenReturn ("(" );
78
-
79
- doAnswer (invocation -> {
80
- Glue glue = invocation .getArgument (0 );
81
- glue .addBeforeHook (hook );
82
- return null ;
83
- }).when (backend ).loadGlue (any (Glue .class ), ArgumentMatchers .anyList ());
84
63
85
64
RuntimeException e = assertThrows (RuntimeException .class ,
86
65
() -> new Runner (bus , Collections .singleton (backend ), objectFactory ,
@@ -111,4 +90,71 @@ public void stop() {
111
90
112
91
}
113
92
}
93
+
94
+ private final static class StubBackend implements Backend {
95
+ private final HookDefinition beforeHook ;
96
+ private final List <String > eventListener ;
97
+
98
+ public StubBackend (HookDefinition beforeHook , List <String > eventListener ) {
99
+ this .beforeHook = beforeHook ;
100
+ this .eventListener = eventListener ;
101
+ }
102
+
103
+ @ Override
104
+ public void loadGlue (Glue glue , List <URI > gluePaths ) {
105
+ glue .addBeforeHook (beforeHook );
106
+ }
107
+
108
+ @ Override
109
+ public void buildWorld () {
110
+ eventListener .add ("buildWorld" );
111
+ }
112
+
113
+ @ Override
114
+ public void disposeWorld () {
115
+ eventListener .add ("disposeWorld" );
116
+ }
117
+
118
+ @ Override
119
+ public Snippet getSnippet () {
120
+ return new TestSnippet ();
121
+ }
122
+ }
123
+
124
+ private static final class MockHookDefinition implements HookDefinition {
125
+ private final String tagExpression ;
126
+ private final String location ;
127
+ private final List <String > eventListener ;
128
+
129
+ public MockHookDefinition (String tagExpression , String location , List <String > eventListener ) {
130
+ this .tagExpression = tagExpression ;
131
+ this .location = location ;
132
+ this .eventListener = eventListener ;
133
+ }
134
+
135
+ @ Override
136
+ public void execute (TestCaseState state ) {
137
+ eventListener .add ("execute" );
138
+ }
139
+
140
+ @ Override
141
+ public String getTagExpression () {
142
+ return tagExpression ;
143
+ }
144
+
145
+ @ Override
146
+ public int getOrder () {
147
+ return 0 ;
148
+ }
149
+
150
+ @ Override
151
+ public boolean isDefinedAt (StackTraceElement stackTraceElement ) {
152
+ return false ;
153
+ }
154
+
155
+ @ Override
156
+ public String getLocation () {
157
+ return location ;
158
+ }
159
+ }
114
160
}
0 commit comments