-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running Test Classes with Inherited @Factory and @DataProvider Annotated Non-Static Methods Fail #2800
Comments
Hi, thanks for the report but what is the usecase which justifies so much complexity in tests? A quick fix could be to forbid such kind of usage. |
The real world use case that I have is that I am performing integration tests for a database metadata retrieval library against multiple database products. There are many tests that need to be run against all supported database platforms where each driver can possibly have many property configurations that effect metadata reporting. The connection information (JDBC URL, JDBC properties, etc.) for all the databases is maintained in a single reference source (CSV) for convenience, which is what is driving the @dataProvider use. The @factory annotation is being used to instantiate the test classes 1) to simply manage the cartesian nature, and 2) allow for database connection caching across tests to improve performance. Further complicating matters is that database drivers often don't play well with each other on the same classpath due to jar hell issues. This often means that the tests need to be run from separate projects (gradle/eclipse) in order to avoid jar hell on the classpath at test runtime. This is what is necessitating having a base AbstractTestGenerator which in my case takes the configuration file and a filter pattern for the configurations, and then actual subclasses for each database type. e.g.
This actually does work btw already in TestNG but requires an unnecessary override of the testFactory method in the subclass i.e.:
But all of this is a bit besides the point, since TestNG already supports this, so I'm not sure why disabling/disallowing it would be a good idea. As mentioned in the initial report, the issue simply seems to lie in the dataProviderClass getting set to not null though never specified on the annotation, and then subsequent code relying on the documented contract that the data provider method should be static if that value is not null. |
TestNG Version
7.6.1
Expected behavior
Tests with inherited @factory and @dataProvider annotated methods (non-static) from an abstract class fail while trying to instantiate the DataProvider.
Actual behavior
AFAICS the proceeding should work according to the docs/javadoc etc.
However this produces the following error:
Is the issue reproducible on runner?
Test case sample
See above.
Thoughts on Issue
Debugging the code it seems that there is an issue where the @dataProvider annotation is processed in JDK15TagFactory which always winds up setting the dataProviderClass member to a non-null value even if the dataProviderClass annotation attribute is not set. The non-null value in this case happens to be the abstract class where the annotation is read. Later on this value gets passed to org.testng.internal.Parameters.findDataProvider as seen in the stack above. This method assumes that dataProviderClass should be null if the dataProviderClass member is not set (at least that's my interpretation given the doc on the annotation and the context of the code) and therefore sets the shouldBeStatic flag, this then forces an attempt to re-instantiate the class, instead of just using the already instantiated instance of the factory class, which is the else case e.g.
Unfortunately, I'm not sure what the actual solution is, since I may be missing some context around the Guice implementation stuff, but at least in this case the logic around setting shouldBeStatic seems wrong, and causes the test case above to fail.
Contribution guidelines
Incase you plan to raise a pull request to fix this issue, please make sure you refer our Contributing section for detailed set of steps.
The text was updated successfully, but these errors were encountered: