From 72b0b940c51d7e96372a68e3701b214adf33f785 Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Fri, 14 Jul 2023 16:14:05 +0530 Subject: [PATCH] Disable Native DI for BeforeSuite methods Closes #2925 --- CHANGES.txt | 1 + .../java/org/testng/internal/Parameters.java | 35 +++++++++++++------ .../java/test/inject/NativeInjectionTest.java | 10 +++--- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index c53b7ada3d..52c6b768b1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-2925: Issue in ITestcontext.getAllTestMethods() with annotation @BeforeSuite (Krishnan Mahadevan) Fixed: GITHUB-2928: The constructor of TestRunner encountered NBC changes in 7.8.0 (Krishnan Mahadevan) Fixed: GITHUB-581: Parameters of nested test suites are overridden(Krishnan Mahadevan) Fixed: GITHUB-727 : Fixing data races (Krishnan Mahadevan) 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 701de725ec..0fb28c6306 100644 --- a/testng-core/src/main/java/org/testng/internal/Parameters.java +++ b/testng-core/src/main/java/org/testng/internal/Parameters.java @@ -6,6 +6,7 @@ import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; import java.util.*; +import java.util.Optional; import org.testng.DataProviderHolder; import org.testng.IDataProviderInterceptor; import org.testng.IDataProviderListener; @@ -65,7 +66,7 @@ public class Parameters { +--------------+--------------+---------+--------+----------+-------------+ | Annotation | ITestContext | XmlTest | Method | Object[] | ITestResult | +--------------+--------------+---------+--------+----------+-------------+ - | BeforeSuite | Yes | Yes | No | No | No | + | BeforeSuite | No | No | No | No | No | +--------------+--------------+---------+--------+----------+-------------+ | BeforeTest | Yes | Yes | No | No | No | +--------------+--------------+---------+--------+----------+-------------+ @@ -75,7 +76,7 @@ public class Parameters { +--------------+--------------+---------+--------+----------+-------------+ | BeforeMethod | Yes | Yes | Yes | Yes | Yes | +--------------+--------------+---------+--------+----------+-------------+ - | AfterSuite | Yes | Yes | No | No | No | + | AfterSuite | No | No | No | No | No | +--------------+--------------+---------+--------+----------+-------------+ | AfterTest | Yes | Yes | No | No | No | +--------------+--------------+---------+--------+----------+-------------+ @@ -96,8 +97,8 @@ public class Parameters { List> beforeAfterMethod = Arrays.asList( ITestContext.class, XmlTest.class, Method.class, Object[].class, ITestResult.class); - mapping.put(BeforeSuite.class.getSimpleName(), ctxTest); - mapping.put(AfterSuite.class.getSimpleName(), ctxTest); + mapping.put(BeforeSuite.class.getSimpleName(), Collections.emptyList()); + mapping.put(AfterSuite.class.getSimpleName(), Collections.emptyList()); mapping.put(BeforeTest.class.getSimpleName(), ctxTest); mapping.put(AfterTest.class.getSimpleName(), ctxTest); @@ -417,13 +418,25 @@ private static void checkParameterTypes( } String errPrefix; if (mapping.containsKey(methodAnnotation)) { - errPrefix = - "Can inject only one of " - + prettyFormat(mapping.get(methodAnnotation)) - + " into a " - + annotation - + " annotated " - + methodName; + boolean nativeInjectionUnsupported = + Optional.ofNullable(mapping.get(methodAnnotation)) + .orElse(Collections.emptyList()) + .isEmpty(); + if (nativeInjectionUnsupported) { + errPrefix = + "Native Injection is NOT supported for @" + + methodAnnotation + + " annotated " + + methodName; + } else { + errPrefix = + "Can inject only one of " + + prettyFormat(mapping.get(methodAnnotation)) + + " into a " + + annotation + + " annotated " + + methodName; + } } else { errPrefix = "Cannot inject " diff --git a/testng-core/src/test/java/test/inject/NativeInjectionTest.java b/testng-core/src/test/java/test/inject/NativeInjectionTest.java index b6b9420373..77d2e54e63 100644 --- a/testng-core/src/test/java/test/inject/NativeInjectionTest.java +++ b/testng-core/src/test/java/test/inject/NativeInjectionTest.java @@ -1,5 +1,6 @@ package test.inject; +import static org.assertj.core.api.Assertions.assertThat; import static test.inject.NativeInjectionTestSamples.*; import org.testng.*; @@ -9,13 +10,13 @@ public class NativeInjectionTest extends SimpleBaseTest { @Test(dataProvider = "getTestData") - public void testBeforeSuiteInjection(Class clazz, String methodName, String expected) { + public void testBeforeSuiteInjection(Class clazz, String methodName, String expected) { TestNG tng = create(clazz); InjectionResultHolder holder = new InjectionResultHolder(); tng.addListener(holder); tng.setGroups("test"); tng.run(); - Assert.assertTrue(holder.getErrorMessage().contains(expected + methodName)); + assertThat(holder.getErrorMessage()).contains(expected + methodName); } @DataProvider @@ -23,11 +24,12 @@ public Object[][] getTestData() { String variant1 = "Can inject only one of into a @%s annotated "; String variant2 = "Can inject only one of into a @%s annotated "; + String variant3 = "Native Injection is NOT supported for @%s annotated "; return new Object[][] { { BadBeforeSuiteSample.class, "beforeSuite", - String.format(variant1, BeforeSuite.class.getSimpleName()) + String.format(variant3, BeforeSuite.class.getSimpleName()) }, { BadBeforeTestSample.class, @@ -62,7 +64,7 @@ public Object[][] getTestData() { { BadAfterSuiteSample.class, "afterSuite", - String.format(variant1, AfterSuite.class.getSimpleName()) + String.format(variant3, AfterSuite.class.getSimpleName()) }, { BadBeforeGroupsSample.class,