diff --git a/CHANGES.txt b/CHANGES.txt index 02b31688a6..47dbcc4f33 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB2255: Ensure test method parameters are visible in BeforeMethod config method (Krishnan Mahadevan) Fixed: GITHUB-2251: NullPointerException at test with timeOut (Krishnan Mahadevan) Fixed: GITHUB-2249: Not abstract super-classes mess up test run order (Sergii Kim) Fixed: GITHUB-2195: NPE Using groups and @Before/@AfterMethod with alwaysRun and dependsOnMethods (Tomas & Julien Herr) diff --git a/src/main/java/org/testng/internal/TestInvoker.java b/src/main/java/org/testng/internal/TestInvoker.java index 30751c6649..c29599261c 100644 --- a/src/main/java/org/testng/internal/TestInvoker.java +++ b/src/main/java/org/testng/internal/TestInvoker.java @@ -509,6 +509,9 @@ private void handleInvocationResults( private ITestResult invokeMethod( TestMethodArguments arguments, XmlSuite suite, FailureContext failureContext) { TestResult testResult = TestResult.newEmptyTestResult(); + testResult.setParameters(arguments.getParameterValues()); + testResult.setParameterIndex(arguments.getParametersIndex()); + testResult.setHost(m_testContext.getHost()); GroupConfigMethodArguments cfgArgs = new GroupConfigMethodArguments.Builder() .forTestMethod(arguments.getTestMethod()) @@ -556,9 +559,6 @@ private ITestResult invokeMethod( invokedMethod = new InvokedMethod(arguments.getInstance(), arguments.getTestMethod(), invokedMethod.getDate(), testResult); - testResult.setParameters(arguments.getParameterValues()); - testResult.setParameterIndex(arguments.getParametersIndex()); - testResult.setHost(m_testContext.getHost()); testResult.setStatus(ITestResult.STARTED); Reporter.setCurrentTestResult(testResult); diff --git a/src/test/java/test/dataprovider/issue2255/IssueTest.java b/src/test/java/test/dataprovider/issue2255/IssueTest.java new file mode 100644 index 0000000000..a16a6ea487 --- /dev/null +++ b/src/test/java/test/dataprovider/issue2255/IssueTest.java @@ -0,0 +1,17 @@ +package test.dataprovider.issue2255; + +import static org.assertj.core.api.Assertions.assertThat; +import org.testng.TestNG; +import org.testng.annotations.Test; +import test.SimpleBaseTest; + +public class IssueTest extends SimpleBaseTest { + + @Test(description = "GITHUB-2255") + public void runTest() { + TestNG testNG = create(SampleTestCase.class); + testNG.setVerbose(2); + testNG.run(); + assertThat(SampleTestCase.data).containsExactly(100, 200); + } +} diff --git a/src/test/java/test/dataprovider/issue2255/SampleTestCase.java b/src/test/java/test/dataprovider/issue2255/SampleTestCase.java new file mode 100644 index 0000000000..340640127a --- /dev/null +++ b/src/test/java/test/dataprovider/issue2255/SampleTestCase.java @@ -0,0 +1,40 @@ +package test.dataprovider.issue2255; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import static org.assertj.core.api.Assertions.assertThat; +import org.testng.ITestResult; +import org.testng.Reporter; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import test.SimpleBaseTest; + +public class SampleTestCase extends SimpleBaseTest { + + static final List data = new ArrayList<>(); + private static final AtomicInteger counter = new AtomicInteger(0); + + @BeforeMethod + public void beforeMethod(ITestResult result) { + Object[] parameters = result.getParameters(); + if (parameters == null || parameters.length == 0) { + throw new IllegalStateException("parameters aren't visible"); + } + data.add((Integer) parameters[0]); + } + + @Test(dataProvider = "dp") + public void testMethod(int i) { + int index = counter.getAndIncrement(); + assertThat(i).isEqualTo(data.get(index)); + Integer value = (Integer) Reporter.getCurrentTestResult().getParameters()[0]; + assertThat(value).isEqualTo(data.get(index)); + } + + @DataProvider(name = "dp") + public Object[][] getData() { + return new Object[][] {{100}, {200}}; + } +} diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index 7fe8807abd..7f854b5f56 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -582,6 +582,7 @@ +