44import io .cucumber .core .backend .Glue ;
55import io .cucumber .core .backend .HookDefinition ;
66import io .cucumber .core .backend .ObjectFactory ;
7+ import io .cucumber .core .backend .Snippet ;
8+ import io .cucumber .core .backend .TestCaseState ;
79import io .cucumber .core .eventbus .EventBus ;
810import io .cucumber .core .feature .TestFeatureParser ;
911import io .cucumber .core .gherkin .Feature ;
1214import io .cucumber .core .runtime .TimeServiceEventBus ;
1315import io .cucumber .core .snippets .TestSnippet ;
1416import org .junit .jupiter .api .Test ;
15- import org .mockito .ArgumentMatchers ;
16- import org .mockito .InOrder ;
1717
18+ import java .net .URI ;
1819import java .time .Clock ;
20+ import java .util .ArrayList ;
1921import java .util .Collections ;
22+ import java .util .List ;
2023import java .util .UUID ;
2124
2225import static org .hamcrest .MatcherAssert .assertThat ;
2326import static org .hamcrest .Matchers .is ;
27+ import static org .junit .jupiter .api .Assertions .assertLinesMatch ;
2428import 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 ;
3029
3130class HookTest {
3231
@@ -44,43 +43,23 @@ class HookTest {
4443 */
4544 @ Test
4645 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 );
4949 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-
6050 Runner runner = new Runner (bus , Collections .singleton (backend ), objectFactory , runtimeOptions );
6151
6252 runner .runPickle (pickle );
6353
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" ));
6855 }
6956
7057 @ Test
7158 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 );
7462 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 ());
8463
8564 RuntimeException e = assertThrows (RuntimeException .class ,
8665 () -> new Runner (bus , Collections .singleton (backend ), objectFactory ,
@@ -111,4 +90,71 @@ public void stop() {
11190
11291 }
11392 }
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+ }
114160}
0 commit comments