Category:
TestNG
@Factory annotation helps in the dynamic execution of test cases. Using @Factory annotation we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.
Example – there are two classes TestClass and the TestFactory class. Because of the @Factory annotation, the test methods in class TestClass will run twice with the data “k1” and “k2”.
public class TestClass{
private String str;
//Constructor
public TestClass(String str) {
this.str = str;
}
@Test
public void TestMethod() {
System.out.println(str);
}
}
==========================
public class TestFactory{
//The test methods in class TestClass will run twice with data “k1” and “k2”
@Factory
public Object[] factoryMethod() {
return new Object[] { new TestClass(“K1”), new TestClass(“k2”) };
}
}