Skip to content

Commit

Permalink
Streamline test parameter visibility in config method
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Mar 2, 2020
1 parent 78a3ce7 commit d480811
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/testng/internal/TestInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/test/dataprovider/issue2255/IssueTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
40 changes: 40 additions & 0 deletions src/test/java/test/dataprovider/issue2255/SampleTestCase.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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}};
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@
<class name="test.dataprovider.DataProviderTest"/>
<class name="test.dataprovider.issue1987.IssueTest"/>
<class name="test.dataprovider.InterceptorTest"/>
<class name="test.dataprovider.issue2255.IssueTest"/>
</classes>
</test>

Expand Down

0 comments on commit d480811

Please sign in to comment.