running depend testng method using @factory

449 views Asked by At

Scenario:-

My Testdata is 1)String A 2) String B 3) String C

Now i have class in which i have data provider and factory and Data provider has 3 value as String

public class TestParent {

    public String tenant;

    @Factory(dataProvider = "myList")
    public TestParent(String s) {
        tenant = s;
    }

    @DataProvider(name = "myList")
    public Object[][] myList() {
        List<String> s = new ArrayList<String>();
        s.add("A");
        s.add("B");
        s.add("C");
        Object[][] objArray = new Object[s.size()][];

        for (int i = 0; i < s.size(); i++) {
            objArray[i] = new Object[1];
            objArray[i][0] = s.get(i);
        }
        return objArray;
    }

    @Test()
    public void setup() {
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = setup)
    public void beforeclass() {
        System.out.print("Hello");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = beforeclass)
    public void afterclass() {
        System.out.print("BYE");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = afterclass)
    public void mm() {
        System.out.print("YES");
        System.out.print(tenant);
        System.out.print("\n");
    }
}

Output i want

A
HelloA
BYEA
YESA
B
HelloB
BYEB
YESB
C
HelloC
BYEC
YESC

1

There are 1 answers

0
Chandan Nayak On BEST ANSWER

Create two separate classes FactoryClass and TestDataClass and then run the FactoryClass from textng.xml

FactoryClass:

public class FactoryClass {

    @Factory(dataProvider = "myList")
    public Object[] createInstances(String str) {
        return new Object[] {new TestData(str)};
    }

    @DataProvider(name = "myList")
    static public Object[][] myList(){
     return new Object[][] {
        new Object[] { new String("A") },
        new Object[] { new String("B") },
      };
    }
} 

TestDataClass:

public class TestData {

    private String tenant;

    public TestData(String s)
    {
        this.tenant=s;
    }

    @Test()
    public void setup( )
    {
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsOnMethods="setup")
    public void beforeclass()
    {
        System.out.print("Hello");
        System.out.print(tenant);
        System.out.print("\n");

    }

    @Test(dependsOnMethods="beforeclass")
    public  void afterclass()
    {
        System.out.print("BYE");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsOnMethods="afterclass")
    public  void mm()
    {
        System.out.print("YES");
        System.out.print(tenant);
        System.out.print("\n");

    }

}

XML File:

<suite name="StackNew" verbose="1">
<test name="test" group-by-instances="true">
    <classes>
        <class name="stack.FactoryClass"></class>
    </classes>
</test>
</suite>

If you Face Sequence issue: Link