Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[TESTNG-343] Test class with @ObjectFactory doesn't get instantiated …

…via the factory
  • Loading branch information...
commit fe62fb2247e28928b19b141b83c4a82fa44e4c68 1 parent 43f186d
DanFabulich authored
View
1  src/main/org/testng/internal/TestNGClassFinder.java
@@ -59,7 +59,6 @@ public TestNGClassFinder(Class[] classes,
}
try {
Object instance = cls.newInstance();
- instanceMap.put(cls, java.util.Arrays.asList(instance));
if (m.getParameterTypes().length > 0 && m.getParameterTypes()[0].equals(ITestContext.class)) {
objectFactory = (IObjectFactory) m.invoke(instance, testContext);
} else {
View
36 test/src/test/objectfactory/CombinedTestAndObjectFactorySample.java
@@ -0,0 +1,36 @@
+package test.objectfactory;
+
+import java.lang.reflect.Constructor;
+
+import org.testng.Assert;
+import org.testng.IObjectFactory;
+import org.testng.annotations.ObjectFactory;
+import org.testng.annotations.Test;
+
+@SuppressWarnings("serial")
+public class CombinedTestAndObjectFactorySample implements IObjectFactory{
+ private boolean configured = false;
+
+ @ObjectFactory public IObjectFactory create() {
+ return new CombinedTestAndObjectFactorySample();
+ }
+
+ @Test public void isConfigured() {
+ Assert.assertTrue(configured, "Should have been configured by object factory");
+ }
+
+ @SuppressWarnings("unchecked")
+ public Object newInstance(Constructor constructor, Object... params) {
+ Object o;
+ try {
+ o = constructor.newInstance(params);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ if (CombinedTestAndObjectFactorySample.class.equals(o.getClass())) {
+ CombinedTestAndObjectFactorySample s = (CombinedTestAndObjectFactorySample) o;
+ s.configured = true;
+ }
+ return o;
+ }
+}
View
23 test/src/test/objectfactory/CombinedTestAndObjectFactoryTest.java
@@ -0,0 +1,23 @@
+package test.objectfactory;
+
+import org.testng.TestNG;
+import org.testng.annotations.Test;
+
+import test.BaseTest;
+
+public class CombinedTestAndObjectFactoryTest extends BaseTest {
+ @Test void combinedTestAndObjectFactory() {
+ addClass(CombinedTestAndObjectFactorySample.class.getName());
+ run();
+ verifyTests("Should have passed", new String[]{"isConfigured"}, getPassedTests());
+ verifyTests("Failures", new String[0], getFailedTests());
+ verifyTests("Skipped", new String[0], getSkippedTests());
+ }
+
+ public static void main(String[] args) {
+ TestNG tng = new TestNG();
+ tng.setTestClasses(new Class[] {CombinedTestAndObjectFactorySample.class});
+ tng.setVerbose(10);
+ tng.run();
+ }
+}
View
1  test/testng.xml
@@ -300,6 +300,7 @@
<test name="Object factory">
<classes>
<class name="test.objectfactory.CustomFactoryTest" />
+ <class name="test.objectfactory.CombinedTestAndObjectFactoryTest" />
</classes>
</test>
Please sign in to comment.
Something went wrong with that request. Please try again.