question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parameterized test class crashes when data provider returns empty array

See original GitHub issue

Let’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:closed
  • Created 7 years ago
  • Comments:21 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ryanlevellcommented, May 19, 2016

@priyanshus getInstance() is null because FactoryMethod#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?

1reaction
priyanshuscommented, May 19, 2016

@cbeust Without using @Factory for dataProvider test result look like this

Total : 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found