From bfdaf6d177c9b02592caa38f46ca098409168e7f Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Mon, 31 Oct 2022 12:45:31 +0530 Subject: [PATCH] Streamline dataprovider invoking in abstract classes Closes #2800 --- CHANGES.txt | 1 + gradle.properties | 2 +- .../java/org/testng/internal/Parameters.java | 3 ++- .../test/dataprovider/DataProviderTest.java | 6 ++++++ .../issue2800/AbstractTestClassGenerator.java | 17 +++++++++++++++++ .../issue2800/TestClassGenerator.java | 3 +++ .../dataprovider/issue2800/TestClassSample.java | 9 +++++++++ 7 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 testng-core/src/test/java/test/dataprovider/issue2800/AbstractTestClassGenerator.java create mode 100644 testng-core/src/test/java/test/dataprovider/issue2800/TestClassGenerator.java create mode 100644 testng-core/src/test/java/test/dataprovider/issue2800/TestClassSample.java diff --git a/CHANGES.txt b/CHANGES.txt index af5e29aa2c..98a5d56078 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-2800: Running Test Classes with Inherited @Factory and @DataProvider Annotated Non-Static Methods Fail (Krishnan Mahadevan) New: Ability to provide custom error message for assertThrows\expectThrows methods (Anatolii Yuzhakov) Fixed: GITHUB-2780: Use SpotBugs instead of abandoned FindBugs Fixed: GITHUB-2801: JUnitReportReporter is too slow diff --git a/gradle.properties b/gradle.properties index 3323d24fa1..f879dc453a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ kotlin.code.style=official # Note: testng.kotlin-library.gradle.kts adds kotlin-stdlib for testImplementation kotlin.stdlib.default.dependency=false -testng.version=7.6.1 +testng.version=7.6.2 group=org.testng diff --git a/testng-core/src/main/java/org/testng/internal/Parameters.java b/testng-core/src/main/java/org/testng/internal/Parameters.java index feb409df7b..90bf412d95 100644 --- a/testng-core/src/main/java/org/testng/internal/Parameters.java +++ b/testng-core/src/main/java/org/testng/internal/Parameters.java @@ -610,7 +610,8 @@ private static IDataProviderMethod findDataProvider( Class cls = clazz.getRealClass(); boolean shouldBeStatic = false; - if (dataProviderClass != null) { + boolean isDataProviderClassAbstract = Modifier.isAbstract(cls.getModifiers()); + if (dataProviderClass != null && !isDataProviderClassAbstract) { cls = dataProviderClass; shouldBeStatic = true; } diff --git a/testng-core/src/test/java/test/dataprovider/DataProviderTest.java b/testng-core/src/test/java/test/dataprovider/DataProviderTest.java index c19a11447c..6b2f5058e4 100644 --- a/testng-core/src/test/java/test/dataprovider/DataProviderTest.java +++ b/testng-core/src/test/java/test/dataprovider/DataProviderTest.java @@ -43,6 +43,12 @@ public class DataProviderTest extends SimpleBaseTest { + @Test(description = "GITHUB-2800") + public void testDataProviderFromAbstractClassWhenCoupledWithFactories() { + InvokedMethodNameListener listener = run(test.dataprovider.issue2800.TestClassGenerator.class); + assertThat(listener.getSucceedMethodNames()).containsExactly("hi", "hi"); + } + @Test(description = "GITHUB-1691") public void testDataProviderInfoIgnored() { InvokedMethodNameListener listener = diff --git a/testng-core/src/test/java/test/dataprovider/issue2800/AbstractTestClassGenerator.java b/testng-core/src/test/java/test/dataprovider/issue2800/AbstractTestClassGenerator.java new file mode 100644 index 0000000000..74fd796097 --- /dev/null +++ b/testng-core/src/test/java/test/dataprovider/issue2800/AbstractTestClassGenerator.java @@ -0,0 +1,17 @@ +package test.dataprovider.issue2800; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Factory; + +public abstract class AbstractTestClassGenerator { + + @DataProvider(name = "dataProvider") + public Object[][] dataProvider() { + return new Object[][] {{"foo"}, {"bar"}}; + } + + @Factory(dataProvider = "dataProvider") + public Object[] testFactory(String value) { + return new Object[] {new TestClassSample()}; + } +} diff --git a/testng-core/src/test/java/test/dataprovider/issue2800/TestClassGenerator.java b/testng-core/src/test/java/test/dataprovider/issue2800/TestClassGenerator.java new file mode 100644 index 0000000000..4eb682d6f7 --- /dev/null +++ b/testng-core/src/test/java/test/dataprovider/issue2800/TestClassGenerator.java @@ -0,0 +1,3 @@ +package test.dataprovider.issue2800; + +public class TestClassGenerator extends AbstractTestClassGenerator {} diff --git a/testng-core/src/test/java/test/dataprovider/issue2800/TestClassSample.java b/testng-core/src/test/java/test/dataprovider/issue2800/TestClassSample.java new file mode 100644 index 0000000000..5f7e9b12dd --- /dev/null +++ b/testng-core/src/test/java/test/dataprovider/issue2800/TestClassSample.java @@ -0,0 +1,9 @@ +package test.dataprovider.issue2800; + +import org.testng.annotations.Test; + +public class TestClassSample { + + @Test + public void hi() {} +}