diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a254d5606..d5ac2e6fd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Removed spurious dependencies: - `javax.activation:activation` - `org.glassfish.jaxb:jaxb-runtime` +- [TestNG] Remove spurious Optional\[] from test name ([#2488](https://github.com/cucumber/cucumber-jvm/pull/2488) M.P. Korstanje) ## [7.2.3] (2022-01-13) diff --git a/testng/src/main/java/io/cucumber/testng/FeatureWrapperImpl.java b/testng/src/main/java/io/cucumber/testng/FeatureWrapperImpl.java index 6c3ac121f2..2ce33f40ba 100644 --- a/testng/src/main/java/io/cucumber/testng/FeatureWrapperImpl.java +++ b/testng/src/main/java/io/cucumber/testng/FeatureWrapperImpl.java @@ -12,7 +12,7 @@ final class FeatureWrapperImpl implements FeatureWrapper { @Override public String toString() { - return "\"" + feature.getName() + "\""; + return "\"" + feature.getName().orElse("Unknown") + "\""; } } diff --git a/testng/src/test/java/io/cucumber/testng/AbstractTestNGCucumberTestsTest.java b/testng/src/test/java/io/cucumber/testng/AbstractTestNGCucumberTestsTest.java index 6a3d30de2f..3c91d3fb81 100644 --- a/testng/src/test/java/io/cucumber/testng/AbstractTestNGCucumberTestsTest.java +++ b/testng/src/test/java/io/cucumber/testng/AbstractTestNGCucumberTestsTest.java @@ -1,45 +1,113 @@ package io.cucumber.testng; -import org.testng.Assert; +import org.testng.IInvokedMethod; +import org.testng.IInvokedMethodListener; +import org.testng.ITestNGMethod; +import org.testng.ITestResult; import org.testng.TestNG; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import static java.util.Arrays.asList; +import static java.util.Collections.frequency; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; @Test public final class AbstractTestNGCucumberTestsTest { - private List invokedConfigurationMethodNames; - private List invokedTestMethodNames; + private final InvokedMethodListener listener = new InvokedMethodListener(); @BeforeClass(alwaysRun = true) public void setUp() { - InvokedMethodListener icml = new InvokedMethodListener(); + TestNG testNG = new TestNG(); - testNG.addListener(icml); + testNG.addListener(listener); testNG.setGroups("cucumber"); testNG.setTestClasses(new Class[] { RunFeatureWithThreeScenariosTest.class }); testNG.run(); - invokedConfigurationMethodNames = icml.getInvokedConfigurationMethodNames(); - invokedTestMethodNames = icml.getInvokedTestMethodNames(); } @Test public void setUpClassIsInvoked() { - Assert.assertTrue(invokedConfigurationMethodNames.contains("setUpClass"), "setUpClass must be invoked"); + assertTrue(listener.getInvokedTestMethods().stream() + .filter(IInvokedMethod::isConfigurationMethod) + .map(IInvokedMethod::getTestMethod) + .map(ITestNGMethod::getMethodName) + .anyMatch("setUpClass"::equals), + "setUpClass() must be invoked"); } @Test public void tearDownClassIsInvoked() { - Assert.assertTrue(invokedConfigurationMethodNames.contains("tearDownClass"), "tearDownClass must be invoked"); + assertTrue(listener.getInvokedTestMethods().stream() + .filter(IInvokedMethod::isConfigurationMethod) + .map(IInvokedMethod::getTestMethod) + .map(ITestNGMethod::getMethodName) + .anyMatch("tearDownClass"::equals), + "tearDownClass() must be invoked"); } @Test public void runScenarioIsInvokedThreeTimes() { - Assert.assertEquals(Collections.frequency(invokedTestMethodNames, "runScenario"), 3, + List invokedTestMethodNames = listener.getInvokedTestMethods().stream() + .filter(IInvokedMethod::isTestMethod) + .map(IInvokedMethod::getTestMethod) + .map(ITestNGMethod::getMethodName) + .collect(Collectors.toList()); + + assertEquals(frequency(invokedTestMethodNames, "runScenario"), 3, "runScenario() must be invoked three times"); } + @Test + public void providesPickleWrapperAsFirstArgumentWithQuotedStringRepresentation() { + List scenarioNames = listener.getInvokedTestMethods().stream() + .filter(IInvokedMethod::isTestMethod) + .map(IInvokedMethod::getTestResult) + .map(ITestResult::getParameters) + .map(objects -> objects[0]) + .map(o -> (PickleWrapper) o) + .map(Objects::toString) + .collect(Collectors.toList()); + + assertEquals(scenarioNames, asList("\"SC1\"", "\"SC2\"", "\"SC3\"")); + } + + @Test + public void providesFeatureWrapperAsSecondArgumentWithQuotedStringRepresentation() { + List featureNames = listener.getInvokedTestMethods().stream() + .filter(IInvokedMethod::isTestMethod) + .map(IInvokedMethod::getTestResult) + .map(ITestResult::getParameters) + .map(objects -> objects[1]) + .map(o -> (FeatureWrapper) o) + .map(Objects::toString) + .collect(Collectors.toList()); + + assertEquals(frequency(featureNames, "\"A feature containing 3 scenarios\""), 3); + } + + private static final class InvokedMethodListener implements IInvokedMethodListener { + + private final List invokedTestMethods = new ArrayList<>(); + + @Override + public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { + } + + @Override + public void afterInvocation(IInvokedMethod method, ITestResult testResult) { + invokedTestMethods.add(method); + } + + public List getInvokedTestMethods() { + return invokedTestMethods; + } + } } diff --git a/testng/src/test/java/io/cucumber/testng/InvokedMethodListener.java b/testng/src/test/java/io/cucumber/testng/InvokedMethodListener.java deleted file mode 100644 index 54dc5937ac..0000000000 --- a/testng/src/test/java/io/cucumber/testng/InvokedMethodListener.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.cucumber.testng; - -import org.testng.IInvokedMethod; -import org.testng.IInvokedMethodListener; -import org.testng.ITestResult; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public final class InvokedMethodListener implements IInvokedMethodListener { - - private final List invokedConfigurationMethodNames = new ArrayList<>(); - private final List invokedTestMethodNames = new ArrayList<>(); - - @Override - public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { - } - - @Override - public void afterInvocation(IInvokedMethod method, ITestResult testResult) { - String methodName = method.getTestMethod().getMethodName(); - if (method.isConfigurationMethod()) { - invokedConfigurationMethodNames.add(methodName); - } else if (method.isTestMethod()) { - invokedTestMethodNames.add(methodName); - } - } - - public List getInvokedConfigurationMethodNames() { - return Collections.unmodifiableList(invokedConfigurationMethodNames); - } - - public List getInvokedTestMethodNames() { - return Collections.unmodifiableList(invokedTestMethodNames); - } - -} diff --git a/testng/src/test/resources/io/cucumber/testng/three-scenarios.feature b/testng/src/test/resources/io/cucumber/testng/three-scenarios.feature index 8237e1d8a7..64bce2ebb6 100644 --- a/testng/src/test/resources/io/cucumber/testng/three-scenarios.feature +++ b/testng/src/test/resources/io/cucumber/testng/three-scenarios.feature @@ -1,4 +1,4 @@ -Feature: A feature containg 3 scenarios +Feature: A feature containing 3 scenarios Scenario: SC1 Given foo @@ -13,4 +13,4 @@ Feature: A feature containg 3 scenarios Scenario: SC3 Given foo When foo - Then baz \ No newline at end of file + Then baz