What is the use of @Factory annotation in TestNG?

What is the use of @Factory annotation in TestNG?

Hey Emma,

The utilization of the @Factory annotation facilitates the dynamic execution of test cases by enabling the runtime provisioning of parameters to the entire test class. This allows the parameters supplied to be accessed by one or more test methods within that class.

For instance, consider two classes: TestClass and TestFactory. Through the application of the @Factory annotation, the test methods within TestClass will be executed twice, utilizing the data “k1” and “k2.”

public class TestClass {
    private String str;

    // Constructor
    public TestClass(String str) {
        this.str = str;

    public void testMethod() {

public class TestFactory {
    // The test methods in class TestClass will run twice with data "k1" and "k2"
    public Object[] factoryMethod() {
        return new Object[] { new TestClass("K1"), new TestClass("k2") };

In conclusion, it is hoped that this explanation proves beneficial. Please do not hesitate to reach out for any further inquiries; we are here to assist you."