Skip to content

Commit

Permalink
Disable Native DI for BeforeSuite methods
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Jul 14, 2023
1 parent 7dc15af commit 72b0b94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
35 changes: 24 additions & 11 deletions testng-core/src/main/java/org/testng/internal/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 |
+--------------+--------------+---------+--------+----------+-------------+
Expand All @@ -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 |
+--------------+--------------+---------+--------+----------+-------------+
Expand All @@ -96,8 +97,8 @@ public class Parameters {
List<Class<?>> 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);
Expand Down Expand Up @@ -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 "
Expand Down
10 changes: 6 additions & 4 deletions testng-core/src/test/java/test/inject/NativeInjectionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.inject;

import static org.assertj.core.api.Assertions.assertThat;
import static test.inject.NativeInjectionTestSamples.*;

import org.testng.*;
Expand All @@ -9,25 +10,26 @@
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
public Object[][] getTestData() {
String variant1 = "Can inject only one of <ITestContext, XmlTest> into a @%s annotated ";
String variant2 =
"Can inject only one of <ITestContext, XmlTest, Method, Object[], ITestResult> 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,
Expand Down Expand Up @@ -62,7 +64,7 @@ public Object[][] getTestData() {
{
BadAfterSuiteSample.class,
"afterSuite",
String.format(variant1, AfterSuite.class.getSimpleName())
String.format(variant3, AfterSuite.class.getSimpleName())
},
{
BadBeforeGroupsSample.class,
Expand Down

0 comments on commit 72b0b94

Please sign in to comment.