Best Testng code snippet using org.testng.TestNG.setObjectFactory
Source:CustomFactoryTest.java
...12public class CustomFactoryTest {13 @Test14 public void setFactoryOnTestNG() {15 XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");16 // suite.setObjectFactory(new LoggingObjectFactory());17 TestNG tng = TestHelper.createTestNG(suite);18 tng.setObjectFactory(LoggingObjectFactory.class);19 tng.run();20 assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "21 + LoggingObjectFactory.invoked + " times";22 }23 @AfterMethod24 public void resetCount() {25 LoggingObjectFactory.invoked = 0;26 }27 @Test28 public void setFactoryOnSuite() {29 XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");30 suite.setObjectFactory(new LoggingObjectFactory());31 TestNG tng = TestHelper.createTestNG(suite);32 tng.run();33 assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "34 + LoggingObjectFactory.invoked + " times";35 }36 @Test(enabled = false, description = "This broke after I made the change to enable AbstractTest")37 public void setFactoryByAnnotation() {38 XmlSuite suite = TestHelper.createSuite("test.objectfactory.Simple", "objectfactory");39 suite.getTests().get(0).getXmlClasses()40 .add(new XmlClass("test.objectfactory.MyFactoryFactory"));41 TestNG tng = TestHelper.createTestNG(suite);42 tng.run();43 assert LoggingObjectFactory.invoked == 1 : "Logging factory invoked "44 + LoggingObjectFactory.invoked + " times";...
Source:ObjectFactory2Test.java
...13 private void testFactory(boolean onSuite) {14 XmlSuite suite = TestHelper.createSuite(ClassObjectFactorySampleTest.class.getName(),15 "Test IObjectFactory2");16 TestNG tng = TestHelper.createTestNG(suite);17 if (onSuite) suite.setObjectFactory(new ClassObjectFactory());18 else tng.setObjectFactory(ClassObjectFactory.class);19 ClassObjectFactorySampleTest.m_n = 0;20 tng.run();21 Assert.assertEquals(ClassObjectFactorySampleTest.m_n, 42);22 }23 @Test24 public void factoryOnSuiteShouldWork() {25 testFactory(true /* on suite object */);26 }27 @Test28 public void factoryOnTestNGShouldWork() {29 testFactory(false /* on TestNG object */);30 }31}...
setObjectFactory
Using AI Code Generation
1package com.example.testng;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import java.util.ArrayList;5import java.util.List;6public class TestNGObjectFactoryExample {7 public static void main(String[] args) {8 XmlSuite suite = new XmlSuite();9 suite.setName("TestNGObjectFactoryExample");10 List<XmlSuite> suites = new ArrayList<>();11 suites.add(suite);12 TestNG testng = new TestNG();13 testng.setXmlSuites(suites);14 testng.setObjectFactory(new CustomObjectFactory());15 testng.run();16 }17}18package com.example.testng;19import org.testng.IObjectFactory;20import org.testng.ITestContext;21import org.testng.annotations.ITestAnnotation;22import java.lang.reflect.Constructor;23import java.lang.reflect.Method;24public class CustomObjectFactory implements IObjectFactory {25 public Object newInstance(Constructor constructor, Object... objects) {26 System.out.println("newInstance(Constructor constructor, Object... objects) method is invoked");27 return null;28 }29 public Object newInstance(Method method, Object... objects) {30 System.out.println("newInstance(Method method, Object... objects) method is invoked");31 return null;32 }33 public Object newInstance(Class aClass, ITestContext iTestContext) {34 System.out.println("newInstance(Class aClass, ITestContext iTestContext) method is invoked");35 return null;36 }37 public Object newInstance(Class aClass, ITestContext iTestContext, ITestAnnotation iTestAnnotation, Method method, Object... objects) {38 System.out.println("newInstance(Class aClass, ITestContext iTestContext, ITestAnnotation iTestAnnotation, Method method, Object... objects) method is invoked");39 return null;40 }41}42newInstance(Constructor constructor, Object... objects) method is invoked43newInstance(Method method, Object... objects) method is invoked44newInstance(Class aClass, ITestContext iTestContext) method is invoked45newInstance(Class aClass, ITestContext iTestContext, ITestAnnotation iTestAnnotation, Method method,
setObjectFactory
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.TestNG;3public class TestNGTest {4public void testObjectFactory() {5TestNG testng = new TestNG();6testng.setObjectFactory(new ObjectFactory());7testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class });8testng.run();9}10}11import org.testng.IObjectFactory;12import org.testng.ITestContext;13import org.testng.annotations.ObjectFactory;14public class ObjectFactory implements IObjectFactory {15public Object createObject(final Class clazz, final ITestContext context) {16Object obj = null;17try {18obj = clazz.newInstance();19} catch (final Exception e) {20throw new RuntimeException("Cannot create object for class " + clazz, e);21}22return obj;23}24}25import org.testng.annotations.Test;26public class TestClass1 {27public void test1() {28System.out.println("test1");29}30}31import org.testng.annotations.Test;32public class TestClass2 {33public void test2() {34System.out.println("test2");35}36}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!