Skip to content

Commit 9a69e52

Browse files
committed
Disable Native DI for BeforeSuite methods
Closes testng-team#2925
1 parent 7dc15af commit 9a69e52

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-2925: Issue in ITestcontext.getAllTestMethods() with annotation @BeforeSuite (Krishnan Mahadevan)
23
Fixed: GITHUB-2928: The constructor of TestRunner encountered NBC changes in 7.8.0 (Krishnan Mahadevan)
34
Fixed: GITHUB-581: Parameters of nested test suites are overridden(Krishnan Mahadevan)
45
Fixed: GITHUB-727 : Fixing data races (Krishnan Mahadevan)

testng-core/src/main/java/org/testng/internal/Parameters.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class Parameters {
6565
+--------------+--------------+---------+--------+----------+-------------+
6666
| Annotation | ITestContext | XmlTest | Method | Object[] | ITestResult |
6767
+--------------+--------------+---------+--------+----------+-------------+
68-
| BeforeSuite | Yes | Yes | No | No | No |
68+
| BeforeSuite | No | No | No | No | No |
6969
+--------------+--------------+---------+--------+----------+-------------+
7070
| BeforeTest | Yes | Yes | No | No | No |
7171
+--------------+--------------+---------+--------+----------+-------------+
@@ -75,7 +75,7 @@ public class Parameters {
7575
+--------------+--------------+---------+--------+----------+-------------+
7676
| BeforeMethod | Yes | Yes | Yes | Yes | Yes |
7777
+--------------+--------------+---------+--------+----------+-------------+
78-
| AfterSuite | Yes | Yes | No | No | No |
78+
| AfterSuite | No | No | No | No | No |
7979
+--------------+--------------+---------+--------+----------+-------------+
8080
| AfterTest | Yes | Yes | No | No | No |
8181
+--------------+--------------+---------+--------+----------+-------------+
@@ -96,8 +96,8 @@ public class Parameters {
9696
List<Class<?>> beforeAfterMethod =
9797
Arrays.asList(
9898
ITestContext.class, XmlTest.class, Method.class, Object[].class, ITestResult.class);
99-
mapping.put(BeforeSuite.class.getSimpleName(), ctxTest);
100-
mapping.put(AfterSuite.class.getSimpleName(), ctxTest);
99+
mapping.put(BeforeSuite.class.getSimpleName(), Collections.emptyList());
100+
mapping.put(AfterSuite.class.getSimpleName(), Collections.emptyList());
101101

102102
mapping.put(BeforeTest.class.getSimpleName(), ctxTest);
103103
mapping.put(AfterTest.class.getSimpleName(), ctxTest);
@@ -417,13 +417,22 @@ private static void checkParameterTypes(
417417
}
418418
String errPrefix;
419419
if (mapping.containsKey(methodAnnotation)) {
420-
errPrefix =
421-
"Can inject only one of "
422-
+ prettyFormat(mapping.get(methodAnnotation))
423-
+ " into a "
424-
+ annotation
425-
+ " annotated "
426-
+ methodName;
420+
boolean nativeInjectionUnsupported = mapping.get(methodAnnotation).isEmpty();
421+
if (nativeInjectionUnsupported) {
422+
errPrefix =
423+
"Native Injection is NOT supported for @"
424+
+ methodAnnotation
425+
+ " annotated "
426+
+ methodName;
427+
} else {
428+
errPrefix =
429+
"Can inject only one of "
430+
+ prettyFormat(mapping.get(methodAnnotation))
431+
+ " into a "
432+
+ annotation
433+
+ " annotated "
434+
+ methodName;
435+
}
427436
} else {
428437
errPrefix =
429438
"Cannot inject "

testng-core/src/test/java/test/inject/NativeInjectionTest.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package test.inject;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
34
import static test.inject.NativeInjectionTestSamples.*;
45

56
import org.testng.*;
@@ -9,25 +10,26 @@
910
public class NativeInjectionTest extends SimpleBaseTest {
1011

1112
@Test(dataProvider = "getTestData")
12-
public void testBeforeSuiteInjection(Class clazz, String methodName, String expected) {
13+
public void testBeforeSuiteInjection(Class<?> clazz, String methodName, String expected) {
1314
TestNG tng = create(clazz);
1415
InjectionResultHolder holder = new InjectionResultHolder();
1516
tng.addListener(holder);
1617
tng.setGroups("test");
1718
tng.run();
18-
Assert.assertTrue(holder.getErrorMessage().contains(expected + methodName));
19+
assertThat(holder.getErrorMessage()).contains(expected + methodName);
1920
}
2021

2122
@DataProvider
2223
public Object[][] getTestData() {
2324
String variant1 = "Can inject only one of <ITestContext, XmlTest> into a @%s annotated ";
2425
String variant2 =
2526
"Can inject only one of <ITestContext, XmlTest, Method, Object[], ITestResult> into a @%s annotated ";
27+
String variant3 = "Native Injection is NOT supported for @%s annotated ";
2628
return new Object[][] {
2729
{
2830
BadBeforeSuiteSample.class,
2931
"beforeSuite",
30-
String.format(variant1, BeforeSuite.class.getSimpleName())
32+
String.format(variant3, BeforeSuite.class.getSimpleName())
3133
},
3234
{
3335
BadBeforeTestSample.class,
@@ -62,7 +64,7 @@ public Object[][] getTestData() {
6264
{
6365
BadAfterSuiteSample.class,
6466
"afterSuite",
65-
String.format(variant1, AfterSuite.class.getSimpleName())
67+
String.format(variant3, AfterSuite.class.getSimpleName())
6668
},
6769
{
6870
BadBeforeGroupsSample.class,

0 commit comments

Comments
 (0)