Best Testng code snippet using org.testng.Interface IInstanceInfo
Source:IInstanceFactory.java
1package test.factory;2import org.testng.Assert;3import org.testng.IInstanceInfo;4public class IInstanceFactory {5 private static boolean m_invoked= false;6 /**7 * @testng.factory8 */9 public Object[] createObjects() {10 return new IInstanceInfo[] {11 new MyInstanceInfo(TestInterface.class, new TestInterfaceImpl())12 };13 }14 15 /**16 * @testng.configuration afterTest="true"17 */18 public void afterVerification() {19 Assert.assertTrue(m_invoked, "implementation of TestInterface test method should have been invoked");20 }21 22 public static class MyInstanceInfo implements IInstanceInfo {23 private Class m_clazz;24 private Object m_instance;25 26 public MyInstanceInfo(Class clazz, Object instance) {27 m_clazz= clazz;28 m_instance= instance;29 }30 public Object getInstance() {31 return m_instance;32 }33 public Class getInstanceClass() {34 return m_clazz;35 }36 }37 38 public static interface TestInterface {39 /**40 * @testng.test41 */42 void testMethod();43 }44 45 public static class TestInterfaceImpl implements TestInterface {46 public void testMethod() {47 m_invoked= true;48 }49 50 }51}...
Source:IInstanceInfo.java
1package org.testng;2/**3 * This class defines a pair of instance/class. A method with @Factory4 * can return an array of these objects instead of Object[] so that5 * instances can be dynamic proxies or mock objects and still provide6 * enough information to TestNG to figure out what classes the7 * annotations should be looked up in.8 *9 * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>10 */11public interface IInstanceInfo {12 /**13 * @return The instance on which the tests will be invoked.14 */15 public Object getInstance();16 /**17 * @return The class on which the TestNG annotations should be looked for.18 */19 public Class getInstanceClass();20}...
Interface IInstanceInfo
Using AI Code Generation
1public class TestNGTest {2 public void test() throws Exception {3 Class<?> testClass = Class.forName("com.example.TestClass");4 Constructor<?> constructor = testClass.getConstructor();5 Method method = testClass.getMethod("test");6 Object testInstance = constructor.newInstance();7 method.invoke(testInstance);8 }9}10package com.example;11import org.testng.IInstanceInfo;12import org.testng.ITestContext;13import org.testng.ITestNGMethod;14import org.testng.ITestResult;15import org.testng.annotations.Test;16import org.testng.internal.ConstructorOrMethod;17import java.lang.reflect.Constructor;18import java.lang.reflect.Method;19public class TestNGTest {20 public void test() throws Exception {21 Class<?> testClass = Class.forName("com.example.TestClass");22 Constructor<?> constructor = testClass.getConstructor();23 Method method = testClass.getMethod("test");24 Object testInstance = constructor.newInstance();25 IInstanceInfo instanceInfo = new IInstanceInfo() {26 public Object getInstance() {27 return testInstance;28 }29 public ConstructorOrMethod getConstructorOrMethod() {30 return new ConstructorOrMethod(method);31 }32 public ITestContext getTestContext() {33 return null;34 }35 public ITestResult getTestResult() {36 return null;37 }38 public ITestNGMethod getTestNGMethod() {39 return null;40 }41 };42 method.invoke(testInstance, instanceInfo);43 }44}45package com.example;46import org.testng.IInstanceInfo;47import org.testng.ITestContext;48import org.testng.ITestNGMethod;49import org.testng.ITestResult;50import org.testng.annotations.Test;51import
Interface IInstanceInfo
Using AI Code Generation
1package com.testNG;2import org.testng.IInstanceInfo;3import org.testng.IInvokedMethod;4import org.testng.IInvokedMethodListener;5import org.testng.ITestResult;6public class MyInvokedMethodListener implements IInvokedMethodListener {7 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {8 System.out.println("About to begin executing following method : " + method.getTestMethod().getMethodName());9 }10 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {11 System.out.println("Completed executing following method : " + method.getTestMethod().getMethodName());12 }13}14package com.testNG;15import org.testng.IInvokedMethod;16import org.testng.IInvokedMethodListener;17import org.testng.ITestResult;18public class MyInvokedMethodListener implements IInvokedMethodListener {19 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {20 System.out.println("About to begin executing following method : " + method.getTestMethod().getMethodName());21 }22 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {23 System.out.println("Completed executing following method : " + method.getTestMethod().getMethodName());24 }25}26package com.testNG;27import org.testng.ITestContext;28import org.testng.ITestListener;29import org.testng.ITestResult;30public class MyTestListener implements ITestListener {31 public void onTestStart(ITestResult result) {32 System.out.println((result.getMethod().getMethodName() + " start"));33 }34 public void onTestSuccess(ITestResult result) {35 System.out.println((result.getMethod().getMethodName() + " succeed"));36 }37 public void onTestFailure(ITestResult result) {38 System.out.println((result.getMethod().getMethodName() + " failed
Interface IInstanceInfo
Using AI Code Generation
1public class TestNGTest1 {2 public void test1() {3 System.out.println("Test 1");4 }5 public void test2() {6 System.out.println("Test 2");7 }8 public void test3() {9 System.out.println("Test 3");10 }11}12package com.test;13import org.testng.ITestContext;14import org.testng.ITestNGMethod;15import org.testng.ITestResult;16import org.testng.annotations.Test;17import org.testng.internal.InstanceInfo;18public class TestNGTest2 {19 public void test1() {20 System.out.println("Test 1");21 }22 public void test2() {23 System.out.println("Test 2");24 }25 public void test3() {26 System.out.println("Test 3");27 }28}29package com.test;30import org.testng.ITestContext;31import org.testng.ITestNGMethod;32import org.testng.ITestResult;33import org.testng.annotations.Test;34import org.testng.internal.InstanceInfo;35public class TestNGTest3 {36 public void test1() {37 System.out.println("Test 1");38 }39 public void test2() {40 System.out.println("Test 2");41 }42 public void test3() {43 System.out.println("Test 3");44 }45}46package com.test;47import org.testng.ITestContext;48import org.testng.ITestNGMethod;49import org.testng.ITestResult;50import org.testng.annotations.Test;51import org.testng.internal.InstanceInfo;52public class TestNGTest4 {53 public void test1() {54 System.out.println("Test 1");55 }56 public void test2() {57 System.out.println("Test 2");58 }59 public void test3() {60 System.out.println("Test 3");61 }62}63package com.test;64import org.testng.ITestContext;65import org.testng.ITestNGMethod;66import org.testng.ITestResult;67import org.testng.annotations.Test;68import org.testng.internal.InstanceInfo;69public class TestNGTest5 {70 public void test1() {71 System.out.println("Test
Interface IInstanceInfo
Using AI Code Generation
1public class TestNGDataProvider {2 @Test(dataProvider = "data")3 public void test(String s) {4 System.out.println(s);5 }6 @DataProvider(name = "data")7 public Object[][] data() {8 return new Object[][] { { "a" }, { "b" }, { "c" } };9 }10 @Test(dataProvider = "data2")11 public void test2(String s) {12 System.out.println(s);13 }14 @DataProvider(name = "data2")15 public Object[][] data2() {16 return new Object[][] { { "a" }, { "b" }, { "c" } };17 }18}
Interface IInstanceInfo
Using AI Code Generation
1public class TestNGTest implements ITestNGListener {2 public void onTestStart(ITestResult result) {3 ITestNGMethod method = result.getMethod();4 IInstanceInfo[] instances = method.getInstances();5 for (IInstanceInfo instance : instances) {6 Object object = instance.getInstance();7 if (object instanceof TestNGTest) {8 System.out.println("TestNGTest instance is used");9 }10 }11 }12}13public class TestNGTest implements ITestNGListener {14 public void onTestStart(ITestResult result) {15 ITestNGMethod method = result.getMethod();16 ITestNGMethod[] methods = method.getMethodsDependedUpon();17 for (ITestNGMethod m : methods) {18 if (m.getMethodName().equals("testMethod")) {19 System.out.println("testMethod is used");20 }21 }22 }23}24public class TestNGTest implements ITestNGListener {25 public void onTestStart(ITestResult result) {26 ITestNGMethod method = result.getMethod();27 ITestResult[] results = method.getResults(method);28 for (ITestResult r : results) {29 if (r.getStatus() == ITestResult.FAILURE) {30 System.out.println("test failed");31 }32 }33 }34}35public class TestNGTest implements ITestNGListener {36 public void onTestStart(ITestResult result) {37 ITestNGMethod method = result.getMethod();38 ITestContext context = method.getTestContext();39 if (context.getName().equals("test")) {40 System.out.println("test context is used");41 }42 }43}44public class TestNGTest implements ITestNGListener {45 public void onTestStart(ITestResult result) {46 ITestNGMethod method = result.getMethod();47 IClass clazz = method.getTestClass();48 if (clazz.getName().equals("TestNGTest")) {49 System.out.println("TestNGTest class is used");50 }51 }52}
Interface IInstanceInfo
Using AI Code Generation
1package com.testng;2import org.testng.IInstanceInfo;3import org.testng.ITestContext;4import org.testng.ITestNGMethod;5import org.testng.annotations.DataProvider;6import org.testng.annotations.Test;7public class TestNGDataProviderExample {8 @DataProvider(name = "dataProviderMethod")9 public Object[][] createData(ITestNGMethod method, ITestContext context) {10 IInstanceInfo[] instanceInfos = context.getAllTestMethods()[0].getInstanceInfo();11 for (IInstanceInfo instanceInfo : instanceInfos) {12 System.out.println("Instance Name : " + instanceInfo.getName());13 System.out.println("Instance Hash Code : " + instanceInfo.hashCode());14 }15 return new Object[][] { { "Cedric", new Integer(36) }, { "Anne", new Integer(37) }, };16 }17 @Test(dataProvider = "dataProviderMethod")18 public void verifyData(String n1, Integer n2) {19 System.out.println("n1: " + n1 + " n2: " + n2);20 }21}22package com.testng;23import org.testng.IInstanceInfo;24import org.testng.ITestContext;25import org.testng.annotations.Factory;26import org.testng.annotations.Test;27public class TestNGFactoryExample {28 String name;29 public TestNGFactoryExample(String name) {30 this.name = name;31 }32 @Factory(dataProvider = "dataProviderMethod")33 public Object[] createTest(String name) {34 return new Object[] { new TestNGFactoryExample(name) };35 }36 public void verifyData(ITestContext context) {37 IInstanceInfo[] instanceInfos = context.getAllTestMethods()[0].getInstanceInfo();38 for (IInstanceInfo instanceInfo : instanceInfos) {39 System.out.println("Instance Name : " + instanceInfo.getName());40 System.out.println("Instance Hash Code : " + instanceInfo.hashCode());41 }42 }
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!!