Skip to content

Commit

Permalink
[TestNG] Remove spurious Optional[<Feature Name>] from test name
Browse files Browse the repository at this point in the history
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"]
```
  • Loading branch information
mpkorstanje committed Feb 16, 2022
1 parent 65dfc97 commit 1c25dd3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\[<Feature Name>] from test name ([#2488](https://github.com/cucumber/cucumber-jvm/pull/2488) M.P. Korstanje)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class FeatureWrapperImpl implements FeatureWrapper {

@Override
public String toString() {
return "\"" + feature.getName() + "\"";
return "\"" + feature.getName().orElse("Unknown") + "\"";
}

}
Original file line number Diff line number Diff line change
@@ -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<String> invokedConfigurationMethodNames;
private List<String> 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<String> 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<String> 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<String> 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<IInvokedMethod> invokedTestMethods = new ArrayList<>();

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
invokedTestMethods.add(method);
}

public List<IInvokedMethod> getInvokedTestMethods() {
return invokedTestMethods;
}
}
}
38 changes: 0 additions & 38 deletions testng/src/test/java/io/cucumber/testng/InvokedMethodListener.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature: A feature containg 3 scenarios
Feature: A feature containing 3 scenarios

Scenario: SC1
Given foo
Expand All @@ -13,4 +13,4 @@ Feature: A feature containg 3 scenarios
Scenario: SC3
Given foo
When foo
Then baz
Then baz

0 comments on commit 1c25dd3

Please sign in to comment.