Skip to content

Commit 3c2d29d

Browse files
authored
[TestNG] Remove spurious Optional[<Feature Name>] from test name (#2488)
When executing tests, the test name would be displayed as: ``` RunCucumberTest.runScenario["Another Addition", "Optional[Basic Arithmetic]"] ``` Which should have been: ``` RunCucumberTest.runScenario["Many additions", "Basic Arithmetic"] ```
1 parent 27befc8 commit 3c2d29d

File tree

5 files changed

+83
-52
lines changed

5 files changed

+83
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2525
- Removed spurious dependencies:
2626
- `javax.activation:activation`
2727
- `org.glassfish.jaxb:jaxb-runtime`
28+
- [TestNG] Remove spurious Optional\[<Feature Name>] from test name ([#2488](https://github.com/cucumber/cucumber-jvm/pull/2488) M.P. Korstanje)
2829

2930
## [7.2.3] (2022-01-13)
3031

testng/src/main/java/io/cucumber/testng/FeatureWrapperImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class FeatureWrapperImpl implements FeatureWrapper {
1212

1313
@Override
1414
public String toString() {
15-
return "\"" + feature.getName() + "\"";
15+
return "\"" + feature.getName().orElse("Unknown") + "\"";
1616
}
1717

1818
}
Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,113 @@
11
package io.cucumber.testng;
22

3-
import org.testng.Assert;
3+
import org.testng.IInvokedMethod;
4+
import org.testng.IInvokedMethodListener;
5+
import org.testng.ITestNGMethod;
6+
import org.testng.ITestResult;
47
import org.testng.TestNG;
58
import org.testng.annotations.BeforeClass;
69
import org.testng.annotations.Test;
710

8-
import java.util.Collections;
11+
import java.util.ArrayList;
912
import java.util.List;
13+
import java.util.Objects;
14+
import java.util.stream.Collectors;
15+
16+
import static java.util.Arrays.asList;
17+
import static java.util.Collections.frequency;
18+
import static org.testng.Assert.assertEquals;
19+
import static org.testng.Assert.assertTrue;
1020

1121
@Test
1222
public final class AbstractTestNGCucumberTestsTest {
1323

14-
private List<String> invokedConfigurationMethodNames;
15-
private List<String> invokedTestMethodNames;
24+
private final InvokedMethodListener listener = new InvokedMethodListener();
1625

1726
@BeforeClass(alwaysRun = true)
1827
public void setUp() {
19-
InvokedMethodListener icml = new InvokedMethodListener();
28+
2029
TestNG testNG = new TestNG();
21-
testNG.addListener(icml);
30+
testNG.addListener(listener);
2231
testNG.setGroups("cucumber");
2332
testNG.setTestClasses(new Class[] { RunFeatureWithThreeScenariosTest.class });
2433
testNG.run();
25-
invokedConfigurationMethodNames = icml.getInvokedConfigurationMethodNames();
26-
invokedTestMethodNames = icml.getInvokedTestMethodNames();
2734
}
2835

2936
@Test
3037
public void setUpClassIsInvoked() {
31-
Assert.assertTrue(invokedConfigurationMethodNames.contains("setUpClass"), "setUpClass must be invoked");
38+
assertTrue(listener.getInvokedTestMethods().stream()
39+
.filter(IInvokedMethod::isConfigurationMethod)
40+
.map(IInvokedMethod::getTestMethod)
41+
.map(ITestNGMethod::getMethodName)
42+
.anyMatch("setUpClass"::equals),
43+
"setUpClass() must be invoked");
3244
}
3345

3446
@Test
3547
public void tearDownClassIsInvoked() {
36-
Assert.assertTrue(invokedConfigurationMethodNames.contains("tearDownClass"), "tearDownClass must be invoked");
48+
assertTrue(listener.getInvokedTestMethods().stream()
49+
.filter(IInvokedMethod::isConfigurationMethod)
50+
.map(IInvokedMethod::getTestMethod)
51+
.map(ITestNGMethod::getMethodName)
52+
.anyMatch("tearDownClass"::equals),
53+
"tearDownClass() must be invoked");
3754
}
3855

3956
@Test
4057
public void runScenarioIsInvokedThreeTimes() {
41-
Assert.assertEquals(Collections.frequency(invokedTestMethodNames, "runScenario"), 3,
58+
List<String> invokedTestMethodNames = listener.getInvokedTestMethods().stream()
59+
.filter(IInvokedMethod::isTestMethod)
60+
.map(IInvokedMethod::getTestMethod)
61+
.map(ITestNGMethod::getMethodName)
62+
.collect(Collectors.toList());
63+
64+
assertEquals(frequency(invokedTestMethodNames, "runScenario"), 3,
4265
"runScenario() must be invoked three times");
4366
}
4467

68+
@Test
69+
public void providesPickleWrapperAsFirstArgumentWithQuotedStringRepresentation() {
70+
List<String> scenarioNames = listener.getInvokedTestMethods().stream()
71+
.filter(IInvokedMethod::isTestMethod)
72+
.map(IInvokedMethod::getTestResult)
73+
.map(ITestResult::getParameters)
74+
.map(objects -> objects[0])
75+
.map(o -> (PickleWrapper) o)
76+
.map(Objects::toString)
77+
.collect(Collectors.toList());
78+
79+
assertEquals(scenarioNames, asList("\"SC1\"", "\"SC2\"", "\"SC3\""));
80+
}
81+
82+
@Test
83+
public void providesFeatureWrapperAsSecondArgumentWithQuotedStringRepresentation() {
84+
List<String> featureNames = listener.getInvokedTestMethods().stream()
85+
.filter(IInvokedMethod::isTestMethod)
86+
.map(IInvokedMethod::getTestResult)
87+
.map(ITestResult::getParameters)
88+
.map(objects -> objects[1])
89+
.map(o -> (FeatureWrapper) o)
90+
.map(Objects::toString)
91+
.collect(Collectors.toList());
92+
93+
assertEquals(frequency(featureNames, "\"A feature containing 3 scenarios\""), 3);
94+
}
95+
96+
private static final class InvokedMethodListener implements IInvokedMethodListener {
97+
98+
private final List<IInvokedMethod> invokedTestMethods = new ArrayList<>();
99+
100+
@Override
101+
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
102+
}
103+
104+
@Override
105+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
106+
invokedTestMethods.add(method);
107+
}
108+
109+
public List<IInvokedMethod> getInvokedTestMethods() {
110+
return invokedTestMethods;
111+
}
112+
}
45113
}

testng/src/test/java/io/cucumber/testng/InvokedMethodListener.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

testng/src/test/resources/io/cucumber/testng/three-scenarios.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Feature: A feature containg 3 scenarios
1+
Feature: A feature containing 3 scenarios
22

33
Scenario: SC1
44
Given foo
@@ -13,4 +13,4 @@ Feature: A feature containg 3 scenarios
1313
Scenario: SC3
1414
Given foo
1515
When foo
16-
Then baz
16+
Then baz

0 commit comments

Comments
 (0)