[testng] 176/355: Add constructor injection for DataProvider

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:20:02 UTC 2015


This is an automated email from the git hooks/post-receive script.

eugene-guest pushed a commit to annotated tag OpenBSD
in repository testng.

commit 5816aa6e67aa9ae07024c7cbf99f6ea845e59990
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date:   Wed Apr 8 10:19:45 2015 +0200

    Add constructor injection for DataProvider
---
 doc/documentation-main.html                                    |  2 +-
 src/main/java/org/testng/internal/ClassImpl.java               |  4 +---
 src/main/java/org/testng/internal/Parameters.java              |  6 ++----
 ...njectionProvider.java => ConstructorInjectionProvider.java} | 10 +++++++---
 .../{InjectionProvider.java => FieldInjectionProvider.java}    |  2 +-
 .../java/test/dataprovider/StaticDataProviderSampleTest.java   |  9 +++++++--
 6 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/doc/documentation-main.html b/doc/documentation-main.html
index 251ec4e..206cc2b 100644
--- a/doc/documentation-main.html
+++ b/doc/documentation-main.html
@@ -1248,7 +1248,7 @@ public class MyTest {
 }
 </pre>
 
-The data provider supports injection too, but only field one. TestNG will use the test context for the injection.
+The data provider supports injection too. TestNG will use the test context for the injection.
 
 The Data Provider method can return one of the following two types:
 
diff --git a/src/main/java/org/testng/internal/ClassImpl.java b/src/main/java/org/testng/internal/ClassImpl.java
index ddf05f5..460da62 100755
--- a/src/main/java/org/testng/internal/ClassImpl.java
+++ b/src/main/java/org/testng/internal/ClassImpl.java
@@ -128,10 +128,8 @@ public class ClassImpl implements IClass {
    */
   @SuppressWarnings("unchecked")
   private Object getInstanceFromGuice() {
-    Annotation annotation = AnnotationHelper.findAnnotationSuperClasses(Guice.class, m_class);
-    if (annotation == null) return null;
-
     Injector injector = m_testContext.getInjector(this);
+    if (injector == null) return null;
     return injector.getInstance(m_class);
   }
 
diff --git a/src/main/java/org/testng/internal/Parameters.java b/src/main/java/org/testng/internal/Parameters.java
index 16548bb..5306f76 100755
--- a/src/main/java/org/testng/internal/Parameters.java
+++ b/src/main/java/org/testng/internal/Parameters.java
@@ -324,8 +324,6 @@ public class Parameters {
   {
     DataProviderHolder result = null;
 
-    Injector injector = context.getInjector(clazz);
-
     Class cls = clazz.getRealClass();
     boolean shouldBeStatic = false;
     if (dataProviderClass != null) {
@@ -337,9 +335,9 @@ public class Parameters {
       IDataProviderAnnotation dp = finder.findAnnotation(m, IDataProviderAnnotation.class);
       if (null != dp && name.equals(getDataProviderName(dp, m))) {
         if (shouldBeStatic && (m.getModifiers() & Modifier.STATIC) == 0) {
-          instance = ClassHelper.newInstance(dataProviderClass);
+          Injector injector = context.getInjector(clazz);
           if (injector != null) {
-            injector.injectMembers(instance);
+            instance = injector.getInstance(dataProviderClass);
           }
         }
 
diff --git a/src/test/java/test/dataprovider/InjectionProvider.java b/src/test/java/test/dataprovider/ConstructorInjectionProvider.java
similarity index 61%
copy from src/test/java/test/dataprovider/InjectionProvider.java
copy to src/test/java/test/dataprovider/ConstructorInjectionProvider.java
index 1923c71..e6f2cbe 100644
--- a/src/test/java/test/dataprovider/InjectionProvider.java
+++ b/src/test/java/test/dataprovider/ConstructorInjectionProvider.java
@@ -5,10 +5,14 @@ import com.google.inject.name.Named;
 
 import org.testng.annotations.DataProvider;
 
-public class InjectionProvider {
+public class ConstructorInjectionProvider {
 
-  @Inject @Named("test")
-  private String value;
+  private final String value;
+
+  @Inject
+  public ConstructorInjectionProvider(@Named("test") String value) {
+    this.value = value;
+  }
 
   @DataProvider(name = "injection")
   public Object[][] create() {
diff --git a/src/test/java/test/dataprovider/InjectionProvider.java b/src/test/java/test/dataprovider/FieldInjectionProvider.java
similarity index 89%
rename from src/test/java/test/dataprovider/InjectionProvider.java
rename to src/test/java/test/dataprovider/FieldInjectionProvider.java
index 1923c71..5c0c54d 100644
--- a/src/test/java/test/dataprovider/InjectionProvider.java
+++ b/src/test/java/test/dataprovider/FieldInjectionProvider.java
@@ -5,7 +5,7 @@ import com.google.inject.name.Named;
 
 import org.testng.annotations.DataProvider;
 
-public class InjectionProvider {
+public class FieldInjectionProvider {
 
   @Inject @Named("test")
   private String value;
diff --git a/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java b/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
index c761c34..3f78c21 100644
--- a/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
+++ b/src/test/java/test/dataprovider/StaticDataProviderSampleTest.java
@@ -20,8 +20,13 @@ public class StaticDataProviderSampleTest {
     Assert.assertEquals(s, "Cedric");
   }
 
-  @Test(dataProvider = "injection", dataProviderClass = InjectionProvider.class)
-  public void verifyInjection(String s) {
+  @Test(dataProvider = "injection", dataProviderClass = FieldInjectionProvider.class)
+  public void verifyFieldInjection(String s) {
+    Assert.assertEquals(s, "Cedric");
+  }
+
+  @Test(dataProvider = "injection", dataProviderClass = ConstructorInjectionProvider.class)
+  public void verifyConstructorInjection(String s) {
     Assert.assertEquals(s, "Cedric");
   }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git



More information about the pkg-java-commits mailing list