Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TestNG] Remove spurious Optional[<Feature Name>] from test name #2488

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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