25
25
import java .util .AbstractMap .SimpleEntry ;
26
26
import java .util .ArrayList ;
27
27
import java .util .Arrays ;
28
+ import java .util .Collections ;
29
+ import java .util .HashSet ;
28
30
import java .util .List ;
29
31
import java .util .Map ;
32
+ import java .util .Set ;
30
33
31
34
import static java .util .Arrays .asList ;
32
35
import static org .mockito .Mockito .doAnswer ;
@@ -69,15 +72,26 @@ public String getClassName() {
69
72
70
73
public static void runFeatureWithFormatter (final CucumberFeature feature , final Map <String , String > stepsToResult , final List <SimpleEntry <String , String >> hooks ,
71
74
final long stepHookDuration , final Formatter formatter , final Reporter reporter ) throws Throwable , FileNotFoundException {
72
- runFeaturesWithFormatter (Arrays .asList (feature ), stepsToResult , hooks , stepHookDuration , formatter , reporter );
75
+ runFeaturesWithFormatter (Arrays .asList (feature ), stepsToResult , Collections .< String , String > emptyMap (), hooks , stepHookDuration , formatter , reporter );
73
76
}
74
77
75
- public static void runFeaturesWithFormatter (final List <CucumberFeature > features , final Map <String , String > stepsToResult , final List <SimpleEntry <String , String >> hooks ,
76
- final long stepHookDuration , final Formatter formatter , final Reporter reporter ) throws Throwable {
78
+ public static void runFeaturesWithFormatter (final List <CucumberFeature > features , final Map <String , String > stepsToResult ,
79
+ final List <SimpleEntry <String , String >> hooks , final long stepHookDuration , final Formatter formatter , final Reporter reporter ) throws Throwable {
80
+ runFeaturesWithFormatter (features , stepsToResult , Collections .<String ,String >emptyMap (), hooks , stepHookDuration , formatter , reporter );
81
+ }
82
+
83
+ public static void runFeatureWithFormatter (final CucumberFeature feature , final Map <String , String > stepsToLocation ,
84
+ final Formatter formatter , final Reporter reporter ) throws Throwable {
85
+ runFeaturesWithFormatter (Arrays .asList (feature ), Collections .<String ,String >emptyMap (), stepsToLocation ,
86
+ Collections .<SimpleEntry <String , String >>emptyList (), 0L , formatter , reporter );
87
+ }
88
+
89
+ private static void runFeaturesWithFormatter (final List <CucumberFeature > features , final Map <String , String > stepsToResult , final Map <String , String > stepsToLocation ,
90
+ final List <SimpleEntry <String , String >> hooks , final long stepHookDuration , final Formatter formatter , final Reporter reporter ) throws Throwable {
77
91
final RuntimeOptions runtimeOptions = new RuntimeOptions (new Env ());
78
92
final ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
79
93
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader (classLoader );
80
- final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps (stepsToResult , hooks );
94
+ final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps (stepsToResult , stepsToLocation , hooks );
81
95
final Runtime runtime = new Runtime (resourceLoader , classLoader , asList (mock (Backend .class )), runtimeOptions , new StopWatch .Stub (stepHookDuration ), glue );
82
96
83
97
for (CucumberFeature feature : features ) {
@@ -87,32 +101,42 @@ public static void runFeaturesWithFormatter(final List<CucumberFeature> features
87
101
formatter .close ();
88
102
}
89
103
90
- private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps (Map <String , String > stepsToResult ,
104
+ private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps (Map <String , String > stepsToResult , Map < String , String > stepsToLocation ,
91
105
final List <SimpleEntry <String , String >> hooks ) throws Throwable {
92
106
RuntimeGlue glue = mock (RuntimeGlue .class );
93
- TestHelper .mockSteps (glue , stepsToResult );
107
+ TestHelper .mockSteps (glue , stepsToResult , stepsToLocation );
94
108
TestHelper .mockHooks (glue , hooks );
95
109
return glue ;
96
110
}
97
111
98
- private static void mockSteps (RuntimeGlue glue , Map <String , String > stepsToResult ) throws Throwable {
99
- for (String stepName : stepsToResult .keySet ()) {
100
- if (!"undefined" .equals (stepsToResult .get (stepName ))) {
112
+ private static void mockSteps (RuntimeGlue glue , Map <String , String > stepsToResult , Map <String , String > stepsToLocation ) throws Throwable {
113
+ for (String stepName : mergeStepSets (stepsToResult , stepsToLocation )) {
114
+ String stepResult = getResultWithDefaultPassed (stepsToResult , stepName );
115
+ if (!"undefined" .equals (stepResult )) {
101
116
StepDefinitionMatch matchStep = mock (StepDefinitionMatch .class );
102
117
when (glue .stepDefinitionMatch (anyString (), TestHelper .stepWithName (stepName ), (I18n ) any ())).thenReturn (matchStep );
103
- if ("pending" .equals (stepsToResult .get (stepName ))) {
104
- doThrow (new PendingException ()).when (matchStep ).runStep ((I18n ) any ());
105
- } else if ("failed" .equals (stepsToResult .get (stepName ))) {
106
- AssertionFailedError error = TestHelper .mockAssertionFailedError ();
107
- doThrow (error ).when (matchStep ).runStep ((I18n ) any ());
108
- } else if (!"passed" .equals (stepsToResult .get (stepName )) &&
109
- !"skipped" .equals (stepsToResult .get (stepName ))) {
110
- fail ("Cannot mock step to the result: " + stepsToResult .get (stepName ));
111
- }
118
+ mockStepResult (stepResult , stepName , matchStep );
119
+ mockStepLocation (getLocationWithDefaultEmptyString (stepsToLocation , stepName ), stepName , matchStep );
112
120
}
113
121
}
114
122
}
115
123
124
+ private static void mockStepResult (String stepResult , String stepName , StepDefinitionMatch matchStep ) throws Throwable {
125
+ if ("pending" .equals (stepResult )) {
126
+ doThrow (new PendingException ()).when (matchStep ).runStep ((I18n ) any ());
127
+ } else if ("failed" .equals (stepResult )) {
128
+ AssertionFailedError error = TestHelper .mockAssertionFailedError ();
129
+ doThrow (error ).when (matchStep ).runStep ((I18n ) any ());
130
+ } else if (!"passed" .equals (stepResult ) &&
131
+ !"skipped" .equals (stepResult )) {
132
+ fail ("Cannot mock step to the result: " + stepResult );
133
+ }
134
+ }
135
+
136
+ private static void mockStepLocation (String stepLocation , String stepName , StepDefinitionMatch matchStep ) {
137
+ when (matchStep .getLocation ()).thenReturn (stepLocation );
138
+ }
139
+
116
140
private static void mockHooks (RuntimeGlue glue , final List <SimpleEntry <String , String >> hooks ) throws Throwable {
117
141
List <HookDefinition > beforeHooks = new ArrayList <HookDefinition >();
118
142
List <HookDefinition > afterHooks = new ArrayList <HookDefinition >();
@@ -165,4 +189,18 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
165
189
public static SimpleEntry <String , String > hookEntry (String type , String result ) {
166
190
return new SimpleEntry <String , String >(type , result );
167
191
}
192
+
193
+ private static Set <String > mergeStepSets (Map <String , String > stepsToResult , Map <String , String > stepsToLocation ) {
194
+ Set <String > steps = new HashSet <String >(stepsToResult .keySet ());
195
+ steps .addAll (stepsToLocation .keySet ());
196
+ return steps ;
197
+ }
198
+
199
+ private static String getResultWithDefaultPassed (Map <String , String > stepsToResult , String step ) {
200
+ return stepsToResult .containsKey (step ) ? stepsToResult .get (step ) : "passed" ;
201
+ }
202
+
203
+ private static String getLocationWithDefaultEmptyString (Map <String , String > stepsToLocation , String step ) {
204
+ return stepsToLocation .containsKey (step ) ? stepsToLocation .get (step ) : "" ;
205
+ }
168
206
}
0 commit comments