Parameterized test class crashes when data provider returns empty array
See original GitHub issueLet’s say I have a test class with a @Factory
-annotated constructor:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class TestTest {
@DataProvider(name = "values")
public static Object[][] values() {
return new Object[][]{{1}, {2}, {3}};
}
private final int value;
@Factory(dataProvider = "values")
public TestTest(int value) {
this.value = value;
}
@Test
public void test() {
assertEquals(value, 2);
}
}
This works perfectly (apart from the assertion errors this silly test throws).
However, if I change the data provider to return an empty array instead (new Object[][]{}
), TestNG crashes:
org.testng.TestNGException:
Can't invoke public void TestTest.test(): either make it static or add a no-args constructor to your class
I expected the test to be ignored, just like it is when the data provider is on method-level instead of class-level. Is this the desired behaviour? If so, why? I ran into this issue because I have data providers that are supposed to be NOPs in certain test configurations.
Issue Analytics
- State:
- Created 7 years ago
- Comments:21 (3 by maintainers)
Top Results From Across the Web
Parameterized test class crashes when data provider returns empty ...
Let's say I have a test class with a @Factory -annotated constructor: import org.testng.annotations.DataProvider; import org.testng.annotations.
Read more >Exploitation of data provider elements inside test as array ...
String)] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level ...
Read more >TestNG throws a misleading error when @Factory method ...
But it turned out that the method generating the test classes (which in reality is quite a bit more complex) was suddenly returning...
Read more >ArgumentOutOfRangeException Class (System)
The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked...
Read more >Understanding null safety - Dart programming language
Here, we want to allow the dairy parameter to accept any string, or the value null , but nothing else. To express that,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@priyanshus
getInstance()
is null becauseFactoryMethod#invoke
returns an empty array when the data provider is empty.This then prevents the loop in
TestNGClassFinder
from adding any instances.Since this is the only place an instance would be found in this case, we have an instance test method with no instance that can be used to call it which causes
Utils#checkInstanceOrStatic
to throw the error.I have code that fixes it, but it would also hide real user errors by preventing the error thrown by
Utils#checkInstanceOrStatic
. So I don’t think it is a good solution yet.@juherr do you know where I could go from here?
@cbeust Without using
@Factory
for dataProvider test result look like thisTotal : 0 Passed: 0 Failed : 0 Skipped : 0
Either we should fix both the cases or let it be same as without @Factory i.e. 0 result.