Skip to content

Commit 045b22c

Browse files
committed
Handle exceptions from data provider
Closes testng-team#2157
1 parent 3b6cd4b commit 045b22c

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-2157: NullPointerException occurs when a Retried test has an exception in DataProvider (Krishnan Mahadevan)
23
New: GITHUB-2111: Provide an interceptor for Data Provider (Krishnan Mahadevan)
34
Fixed: GITHUB-1709: @Ignore doesn't work when used on child class, and parent has multiple @Test methods (Krishnan Mahadevan)
45
New: GITHUB-2118: Default assertion message (Jiong Fu)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.testng;
2+
3+
/**
4+
* Represents any issues that arise out of invoking a data provider method.
5+
*/
6+
public class DataProviderInvocationException extends TestNGException {
7+
8+
public DataProviderInvocationException(String string, Throwable t) {
9+
super(string, t);
10+
}
11+
}

src/main/java/org/testng/internal/TestInvoker.java

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Set;
1616
import java.util.concurrent.atomic.AtomicInteger;
1717
import org.testng.DataProviderHolder;
18+
import org.testng.DataProviderInvocationException;
1819
import org.testng.IClassListener;
1920
import org.testng.IDataProviderListener;
2021
import org.testng.IHookable;
@@ -188,6 +189,14 @@ public FailureContext retryFailed(
188189

189190
ParameterBag bag = handler.createParameters(arguments.getTestMethod(),
190191
arguments.getParameters(), allParameters, testContext);
192+
ITestResult errorResult = bag.errorResult;
193+
194+
if (errorResult != null ) {
195+
Throwable cause = errorResult.getThrowable();
196+
String m = errorResult.getMethod().getMethodName();
197+
String msg = String.format("Encountered problems when gathering parameter values for [%s]. Root cause: ", m);
198+
throw new DataProviderInvocationException(msg, cause);
199+
}
191200
Object[] parameterValues =
192201
Parameters.getParametersFromIndex(Objects.requireNonNull(bag.parameterHolder).parameters,
193202
arguments.getParametersIndex());

src/test/java/test/dataprovider/FailingDataProviderTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package test.dataprovider;
22

3+
import org.testng.DataProviderInvocationException;
34
import org.testng.annotations.Test;
45
import test.InvokedMethodNameListener;
56
import test.SimpleBaseTest;
7+
import test.dataprovider.issue2157.TestClassWithDataProviderThatThrowsExceptions;
68

79
import static org.assertj.core.api.Assertions.assertThat;
810

@@ -33,4 +35,11 @@ public void failingDataProviderAndInvocationCount() {
3335
"testShouldSkipEvenIfSuccessPercentage",
3436
"testShouldSkipEvenIfSuccessPercentage");
3537
}
38+
39+
@Test(description = "GITHUB-2157")
40+
public void abortWhenDataProviderThrowsException() {
41+
InvokedMethodNameListener listener = run(TestClassWithDataProviderThatThrowsExceptions.class);
42+
Throwable cause = listener.getResult("testMethod").getThrowable();
43+
assertThat(cause).isInstanceOf(DataProviderInvocationException.class);
44+
}
3645
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package test.dataprovider.issue2157;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
import org.testng.Assert;
5+
import org.testng.IRetryAnalyzer;
6+
import org.testng.ITestResult;
7+
import org.testng.annotations.DataProvider;
8+
import org.testng.annotations.Test;
9+
10+
public class TestClassWithDataProviderThatThrowsExceptions {
11+
12+
@Test(dataProvider = "dp", retryAnalyzer = SimplyRetry.class)
13+
public void testMethod(String i) {
14+
if ("First".equalsIgnoreCase(i) || "Second".equalsIgnoreCase(i)) {
15+
Assert.fail();
16+
}
17+
}
18+
19+
private static AtomicInteger counter = new AtomicInteger();
20+
21+
@DataProvider(name = "dp")
22+
public static Object[][] dpWithException() {
23+
return new Object[][]{
24+
{foo()},
25+
};
26+
}
27+
28+
private static String foo(){
29+
counter.getAndIncrement();
30+
31+
if(counter.get() == 1){
32+
return "First";
33+
}
34+
if(counter.get() == 2){
35+
return "Second";
36+
}
37+
throw new RuntimeException("TestNG doesn't handle an exception");
38+
}
39+
40+
public static class SimplyRetry implements IRetryAnalyzer {
41+
42+
private static int attempts = 1;
43+
44+
@Override
45+
public boolean retry(ITestResult result) {
46+
return attempts++ != 3;
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)