Skip to content

Commit d135972

Browse files
committed
Fix null pointer exception in TestNG when invalid options are used
Using invalid options through `cucumber.option` would cause the creation of `testNGCucumberRunner` to fail with an exception. However this exception does not terminate TestNGs execution. This caused additional exceptions in the data provider and tear down methods. The additional exceptions were mistaken for the root cause, creating some confusion.
1 parent aeb12cc commit d135972

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

testng/src/main/java/cucumber/api/testng/AbstractTestNGCucumberTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ public void runScenario(PickleEventWrapper pickleWrapper, CucumberFeatureWrapper
2929
*/
3030
@DataProvider
3131
public Object[][] scenarios() {
32+
if (testNGCucumberRunner == null) {
33+
return new Object[0][0];
34+
}
3235
return testNGCucumberRunner.provideScenarios();
3336
}
3437

3538
@AfterClass(alwaysRun = true)
3639
public void tearDownClass() throws Exception {
40+
if (testNGCucumberRunner == null) {
41+
return;
42+
}
3743
testNGCucumberRunner.finish();
3844
}
3945
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cucumber.api.testng;
2+
3+
import cucumber.runtime.CucumberException;
4+
import org.testng.Assert.ThrowingRunnable;
5+
import org.testng.annotations.AfterTest;
6+
import org.testng.annotations.BeforeTest;
7+
import org.testng.annotations.Test;
8+
9+
import static java.util.Arrays.deepEquals;
10+
import static org.testng.Assert.assertThrows;
11+
import static org.testng.Assert.assertTrue;
12+
13+
@Test
14+
public final class AbstractTestNGCucumberTestsInvalidArgument {
15+
16+
private final AbstractTestNGCucumberTests testCase = new AbstractTestNGCucumberTests() {
17+
18+
};
19+
20+
@BeforeTest(alwaysRun = true)
21+
public void setup() {
22+
System.setProperty("cucumber.options", "--invalid-option is invalid");
23+
assertThrows(CucumberException.class, new ThrowingRunnable() {
24+
@Override
25+
public void run() throws Throwable {
26+
testCase.setUpClass();
27+
}
28+
});
29+
}
30+
31+
@AfterTest(alwaysRun = true)
32+
public void tearDown() {
33+
System.setProperty("cucumber.options", "");
34+
}
35+
36+
@Test
37+
public void dataProviderShouldProvideEmptyArrayWhenInvalidArgumentsAreUsed() {
38+
assertTrue(deepEquals(new Object[0][0], testCase.scenarios()));
39+
}
40+
41+
@Test
42+
public void tearDownShouldNotThrowWhenInvalidArgumentsAreUsed() throws Exception {
43+
testCase.tearDownClass();
44+
}
45+
}

0 commit comments

Comments
 (0)