Skip to content

Commit 2ea3787

Browse files
committed
Understandable error message if a formatter needs output location. Closes #148, #232, #269.
1 parent 5db40e6 commit 2ea3787

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

History.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.0.0.RC23...master)
22

3-
* [JUnit] Running with JUnit uses a null formatter by default (instead of a progress formatter). Aslak Hellesøy.
3+
* [Core] Understandable error message if a formatter needs output location. ([#148](https://github.com/cucumber/cucumber-jvm/issues/148), [#232](https://github.com/cucumber/cucumber-jvm/issues/232), [#269](https://github.com/cucumber/cucumber-jvm/issues/269) Aslak Hellesøy)
4+
* [JUnit] Running with JUnit uses a null formatter by default (instead of a progress formatter). (Aslak Hellesøy)
45
* [Clojure] Fix release artifacts so cucumber-clojure can be released. ([#270](https://github.com/cucumber/cucumber-jvm/issues/270) Aslak Hellesøy)
56
* [Java] The @Pending annotation no longer exists. Throw a PendingException instead ([#271](https://github.com/cucumber/cucumber-jvm/issues/271) Aslak Hellesøy)
67

core/src/main/java/cucumber/formatter/FormatterConverter.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,17 @@ public Formatter convert(String formatterString) {
4545
}
4646
Class<? extends Formatter> formatterClass = formatterClass(formatterName);
4747
try {
48-
return instantiate(formatterClass, ctorArg);
48+
return instantiate(formatterString, formatterClass, ctorArg);
4949
} catch (IOException e) {
5050
throw new CucumberException(e);
5151
}
5252
}
5353

54-
private Formatter instantiate(Class<? extends Formatter> formatterClass, Object ctorArg) throws IOException {
55-
Constructor<? extends Formatter> constructor;
56-
54+
private Formatter instantiate(String formatterString, Class<? extends Formatter> formatterClass, Object ctorArg) throws IOException {
5755
for (Class ctorArgClass : CTOR_ARGS) {
58-
constructor = findConstructor(formatterClass, ctorArgClass);
56+
Constructor<? extends Formatter> constructor = findConstructor(formatterClass, ctorArgClass);
5957
if (constructor != null) {
60-
ctorArg = convert(ctorArg, ctorArgClass);
58+
ctorArg = convertOrNull(ctorArg, ctorArgClass);
6159
if (ctorArg != null) {
6260
try {
6361
return constructor.newInstance(ctorArg);
@@ -71,10 +69,13 @@ private Formatter instantiate(Class<? extends Formatter> formatterClass, Object
7169
}
7270
}
7371
}
72+
if (ctorArg == null) {
73+
throw new CucumberException(String.format("You must supply an output argument to %s. Like so: %s:output", formatterString, formatterString));
74+
}
7475
throw new CucumberException(String.format("%s must have a single-argument constructor that takes one of the following: %s", formatterClass, asList(CTOR_ARGS)));
7576
}
7677

77-
private Object convert(Object ctorArg, Class ctorArgClass) throws IOException {
78+
private Object convertOrNull(Object ctorArg, Class ctorArgClass) throws IOException {
7879
if (ctorArgClass.isAssignableFrom(ctorArg.getClass())) {
7980
return ctorArg;
8081
}

core/src/test/java/cucumber/formatter/FormatterConverterTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cucumber.formatter;
22

3+
import cucumber.runtime.CucumberException;
34
import gherkin.formatter.Formatter;
45
import org.junit.Test;
56

@@ -8,6 +9,7 @@
89
import static cucumber.formatter.TempDir.createTempDirectory;
910
import static cucumber.formatter.TempDir.createTempFile;
1011
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.fail;
1113

1214
public class FormatterConverterTest {
1315
private FormatterConverter fc = new FormatterConverter();
@@ -24,6 +26,16 @@ public void instantiates_html_formatter_with_dir_arg() throws IOException {
2426
assertEquals(HTMLFormatter.class, formatter.getClass());
2527
}
2628

29+
@Test
30+
public void fails_to_instantiate_html_formatter_without_dir_arg() throws IOException {
31+
try {
32+
fc.convert("html");
33+
fail();
34+
} catch(CucumberException e) {
35+
assertEquals("You must supply an output argument to html. Like so: html:output", e.getMessage());
36+
}
37+
}
38+
2739
@Test
2840
public void instantiates_pretty_formatter_with_file_arg() throws IOException {
2941
Formatter formatter = fc.convert("pretty:" + createTempFile().getAbsolutePath());

0 commit comments

Comments
 (0)