|
1 | 1 | package io.cucumber.testng;
|
2 | 2 |
|
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; |
4 | 7 | import org.testng.TestNG;
|
5 | 8 | import org.testng.annotations.BeforeClass;
|
6 | 9 | import org.testng.annotations.Test;
|
7 | 10 |
|
8 |
| -import java.util.Collections; |
| 11 | +import java.util.ArrayList; |
9 | 12 | 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; |
10 | 20 |
|
11 | 21 | @Test
|
12 | 22 | public final class AbstractTestNGCucumberTestsTest {
|
13 | 23 |
|
14 |
| - private List<String> invokedConfigurationMethodNames; |
15 |
| - private List<String> invokedTestMethodNames; |
| 24 | + private final InvokedMethodListener listener = new InvokedMethodListener(); |
16 | 25 |
|
17 | 26 | @BeforeClass(alwaysRun = true)
|
18 | 27 | public void setUp() {
|
19 |
| - InvokedMethodListener icml = new InvokedMethodListener(); |
| 28 | + |
20 | 29 | TestNG testNG = new TestNG();
|
21 |
| - testNG.addListener(icml); |
| 30 | + testNG.addListener(listener); |
22 | 31 | testNG.setGroups("cucumber");
|
23 | 32 | testNG.setTestClasses(new Class[] { RunFeatureWithThreeScenariosTest.class });
|
24 | 33 | testNG.run();
|
25 |
| - invokedConfigurationMethodNames = icml.getInvokedConfigurationMethodNames(); |
26 |
| - invokedTestMethodNames = icml.getInvokedTestMethodNames(); |
27 | 34 | }
|
28 | 35 |
|
29 | 36 | @Test
|
30 | 37 | 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"); |
32 | 44 | }
|
33 | 45 |
|
34 | 46 | @Test
|
35 | 47 | 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"); |
37 | 54 | }
|
38 | 55 |
|
39 | 56 | @Test
|
40 | 57 | 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, |
42 | 65 | "runScenario() must be invoked three times");
|
43 | 66 | }
|
44 | 67 |
|
| 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 | + } |
45 | 113 | }
|
0 commit comments