Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Disable Native DI for BeforeSuite methods #2933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 20 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 @@ -65,7 +65,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 +75,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 +96,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 +417,22 @@ 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 = mapping.get(methodAnnotation).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
Loading