1 package test.objectfactory; 2 3 import org.testng.IObjectFactory2; 4 5 import java.lang.reflect.Constructor; 6 7 public class ClassObjectFactory implements IObjectFactory2 { 8 9 @Override newInstance(Class<?> cls)10 public Object newInstance(Class<?> cls) { 11 try { 12 Constructor ctor = cls.getConstructors()[0]; 13 return ctor.newInstance(new Object[] { 42 }); 14 } 15 catch(Exception ex) { 16 ex.printStackTrace(); 17 return null; 18 } 19 } 20 21 } 22