Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented @NoInjection

  • Loading branch information...
commit aba3ac2f9fc3e370339b911598f62c7ffb6752aa 1 parent e157c70
@cbeust authored
View
1  CHANGES.txt
@@ -1,5 +1,6 @@
Current:
+Added: @NoInjection
Added: <test preserve-order="true">
Added: -testnames (command line) and testnames (ant)
Added: New ant task tag: propertyset (Todd Wells)
View
22 doc/documentation-main.html
@@ -1889,6 +1889,28 @@ <h3 class="sourcetitle">Shell</h3>
latter parameter will receive the test method that is about to be invoked.
</ul>
+You can turn off injection with the <tt>@NoInjection</tt> annotation:
+
+<pre class="brush: java; highlight: [9]">
+public class NoInjectionTest {
+
+ @DataProvider(name = "provider")
+ public Object[][] provide() throws Exception {
+ return new Object[][] { { CC.class.getMethod("f") } };
+ }
+
+ @Test(dataProvider = "provider")
+ public void withoutInjection(@NoInjection Method m) {
+ Assert.assertEquals(m.getName(), "f");
+ }
+
+ @Test(dataProvider = "provider")
+ public void withInjection(Method m) {
+ Assert.assertEquals(m.getName(), "withInjection");
+ }
+}
+</pre>
+
<!-------------------------------------
INVOKED METHOD LISTENERS
View
17 src/org/testng/annotations/NoInjection.java
@@ -0,0 +1,17 @@
+package org.testng.annotations;
+
+import static java.lang.annotation.ElementType.PARAMETER;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Turn off TestNG injection for a parameter.
+ *
+ * @author Cedric Beust, July 9th, 2010
+ */
+@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@Target({PARAMETER})
+public @interface NoInjection {
+}
+
View
30 src/org/testng/internal/Invoker.java
@@ -1,6 +1,16 @@
package org.testng.internal;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
import org.testng.IClass;
import org.testng.IHookable;
import org.testng.IInvokedMethod;
@@ -18,6 +28,7 @@
import org.testng.TestException;
import org.testng.TestNGException;
import org.testng.annotations.IConfigurationAnnotation;
+import org.testng.annotations.NoInjection;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.internal.InvokeMethodRunnable.TestNGRuntimeException;
@@ -29,15 +40,6 @@
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Pattern;
-
/**
* This class is responsible for invoking methods:
* - test methods
@@ -1162,8 +1164,16 @@ private ParameterBag createParameters(ITestClass testClass,
List<Object> vResult = Lists.newArrayList();
int i = 0;
for (Class<?> cls : method.getParameterTypes()) {
+ Annotation[] annotations = method.getParameterAnnotations()[i];
+ boolean noInjection = false;
+ for (Annotation a : annotations) {
+ if (a instanceof NoInjection) {
+ noInjection = true;
+ break;
+ }
+ }
Object injected = Parameters.getInjectedParameter(cls, method, context, testResult);
- if (injected != null) {
+ if (injected != null && ! noInjection) {
vResult.add(injected);
} else {
vResult.add(parameterValues[i++]);
View
37 test/src/test/inject/NoInjectionTest.java
@@ -0,0 +1,37 @@
+package test.inject;
+
+import java.lang.reflect.Method;
+
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.NoInjection;
+import org.testng.annotations.Test;
+
+/**
+ * Test the @NoInjection annotation.
+ *
+ * @author cbeust
+ */
+public class NoInjectionTest {
+
+ @DataProvider(name = "provider")
+ public Object[][] provide() throws Exception {
+ return new Object[][] { { CC.class.getMethod("f") } };
+ }
+
+ @Test(dataProvider = "provider")
+ public void withoutInjection(@NoInjection Method m) {
+ Assert.assertEquals(m.getName(), "f");
+ }
+
+ @Test(dataProvider = "provider")
+ public void withInjection(Method m) {
+ Assert.assertEquals(m.getName(), "withInjection");
+ }
+}
+
+class CC {
+
+ public void f() {
+ }
+}
View
2  test/testng-single.xml
@@ -26,7 +26,7 @@
</groups>
<parameter name="count" value="10"/>
<classes>
- <class name="test.dataprovider.ParallelDataProviderTest" />
+ <class name="test.inject.NoInjectionTest" />
<!--
<class name="test.tmp.TestFactory" />
<class name="test.tmp.A" />
View
1  test/testng.xml
@@ -109,6 +109,7 @@
<class name="test.inject.InjectBeforeMethodTest"/>
<class name="test.inject.InjectTestResultTest" />
<class name="test.inject.InjectDataProviderTest"/>
+ <class name="test.inject.NoInjectionTest" />
</classes>
</test>

0 comments on commit aba3ac2

Please sign in to comment.
Something went wrong with that request. Please try again.