Skip to content

Commit 806dd14

Browse files
committed
Move helpers from CucumberPrettyFormatterTest to TestHelper
Move helpers from CucumberPrettyFormatterTest to TestHelper, and refactor to make the helper share implementation in TestHelper.
1 parent ce6a9c9 commit 806dd14

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

core/src/test/java/cucumber/runtime/TestHelper.java

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import java.util.AbstractMap.SimpleEntry;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28+
import java.util.Collections;
29+
import java.util.HashSet;
2830
import java.util.List;
2931
import java.util.Map;
32+
import java.util.Set;
3033

3134
import static java.util.Arrays.asList;
3235
import static org.mockito.Mockito.doAnswer;
@@ -69,15 +72,26 @@ public String getClassName() {
6972

7073
public static void runFeatureWithFormatter(final CucumberFeature feature, final Map<String, String> stepsToResult, final List<SimpleEntry<String, String>> hooks,
7174
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);
7376
}
7477

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 {
7791
final RuntimeOptions runtimeOptions = new RuntimeOptions(new Env());
7892
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
7993
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
80-
final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps(stepsToResult, hooks);
94+
final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps(stepsToResult, stepsToLocation, hooks);
8195
final Runtime runtime = new Runtime(resourceLoader, classLoader, asList(mock(Backend.class)), runtimeOptions, new StopWatch.Stub(stepHookDuration), glue);
8296

8397
for (CucumberFeature feature : features) {
@@ -87,32 +101,42 @@ public static void runFeaturesWithFormatter(final List<CucumberFeature> features
87101
formatter.close();
88102
}
89103

90-
private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(Map<String, String> stepsToResult,
104+
private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(Map<String, String> stepsToResult, Map<String, String> stepsToLocation,
91105
final List<SimpleEntry<String, String>> hooks) throws Throwable {
92106
RuntimeGlue glue = mock(RuntimeGlue.class);
93-
TestHelper.mockSteps(glue, stepsToResult);
107+
TestHelper.mockSteps(glue, stepsToResult, stepsToLocation);
94108
TestHelper.mockHooks(glue, hooks);
95109
return glue;
96110
}
97111

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)) {
101116
StepDefinitionMatch matchStep = mock(StepDefinitionMatch.class);
102117
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);
112120
}
113121
}
114122
}
115123

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+
116140
private static void mockHooks(RuntimeGlue glue, final List<SimpleEntry<String, String>> hooks) throws Throwable {
117141
List<HookDefinition> beforeHooks = new ArrayList<HookDefinition>();
118142
List<HookDefinition> afterHooks = new ArrayList<HookDefinition>();
@@ -165,4 +189,18 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
165189
public static SimpleEntry<String, String> hookEntry(String type, String result) {
166190
return new SimpleEntry<String, String>(type, result);
167191
}
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+
}
168206
}
Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
package cucumber.runtime.formatter;
22

3-
import cucumber.runtime.Backend;
4-
import cucumber.runtime.Env;
5-
import cucumber.runtime.Runtime;
6-
import cucumber.runtime.RuntimeGlue;
7-
import cucumber.runtime.RuntimeOptions;
8-
import cucumber.runtime.StepDefinitionMatch;
9-
import cucumber.runtime.io.ClasspathResourceLoader;
3+
import cucumber.runtime.TestHelper;
104
import cucumber.runtime.model.CucumberFeature;
11-
import gherkin.I18n;
12-
import gherkin.formatter.model.Step;
135
import org.junit.Test;
146

15-
import java.io.IOException;
167
import java.util.HashMap;
178
import java.util.Map;
189

1910
import static cucumber.runtime.TestHelper.feature;
20-
import static java.util.Arrays.asList;
2111
import static org.hamcrest.CoreMatchers.containsString;
2212
import static org.junit.Assert.assertThat;
23-
import static org.mockito.Matchers.any;
24-
import static org.mockito.Matchers.anyString;
25-
import static org.mockito.Matchers.argThat;
26-
import static org.mockito.Mockito.mock;
27-
import static org.mockito.Mockito.when;
2813

2914
public class CucumberPrettyFormatterTest {
3015

3116
@Test
32-
public void should_align_the_indentation_of_location_strings() throws IOException {
17+
public void should_align_the_indentation_of_location_strings() throws Throwable {
3318
CucumberFeature feature = feature("path/test.feature",
3419
"Feature: feature name\n" +
3520
" Scenario: scenario name\n" +
@@ -50,32 +35,12 @@ public void should_align_the_indentation_of_location_strings() throws IOExceptio
5035
" Then third step # path/step_definitions.java:11\n"));
5136
}
5237

53-
private String runFeatureWithPrettyFormatter(final CucumberFeature feature, final Map<String, String> stepsToLocation) throws IOException {
54-
final RuntimeOptions runtimeOptions = new RuntimeOptions(new Env());
55-
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
56-
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
57-
final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps(stepsToLocation);
58-
final Runtime runtime = new Runtime(resourceLoader, classLoader, asList(mock(Backend.class)), runtimeOptions, glue);
38+
private String runFeatureWithPrettyFormatter(final CucumberFeature feature, final Map<String, String> stepsToLocation) throws Throwable {
5939
final StringBuilder out = new StringBuilder();
6040
final CucumberPrettyFormatter prettyFormatter = new CucumberPrettyFormatter(out);
6141
prettyFormatter.setMonochrome(true);
62-
63-
feature.run(prettyFormatter, prettyFormatter, runtime);
64-
42+
TestHelper.runFeatureWithFormatter(feature, stepsToLocation, prettyFormatter, prettyFormatter);
6543
return out.toString();
6644
}
6745

68-
private RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(Map<String, String> stepsToLocation) {
69-
RuntimeGlue glue = mock(RuntimeGlue.class);
70-
for (String stepName : stepsToLocation.keySet()) {
71-
StepDefinitionMatch matchStep = mock(StepDefinitionMatch.class);
72-
when(matchStep.getLocation()).thenReturn(stepsToLocation.get(stepName));
73-
when(glue.stepDefinitionMatch(anyString(), stepWithName(stepName), (I18n) any())).thenReturn(matchStep);
74-
}
75-
return glue;
76-
}
77-
78-
private Step stepWithName(String name) {
79-
return argThat(new StepMatcher(name));
80-
}
8146
}

0 commit comments

Comments
 (0)