2

I am using TestNG @DataProvider Annotation along with @Factory functionality in order to run a flow. When the control goes to @DataProvider method, I want to know from which test class this dataprovider method was called & on basis of that I want to supply test data.

This is my Bean class:

public class Bean {

    private String fName = null;
    private String lName = null;
    private String address = null;

    public synchronized String getfName() {
        return fName;
    }

    public synchronized void setfName(String fName) {
        this.fName = fName;
    }

    public synchronized String getlName() {
        return lName;
    }

    public synchronized void setlName(String lName) {
        this.lName = lName;
    }

    public synchronized String getAddress() {
        return address;
    }

    public synchronized void setAddress(String address) {
        this.address = address;
    }
}

This is my constructor of TestClass with @Factory

@Factory(dataProvider = "dataProvider")
    public TestClass(Bean bean) {

        this.fName = bean.getfName();
        this.lName = bean.getlName();
        this.address = bean.getAddress();
    } 

Here is my DataProvider:

    @DataProvider(name = "dataProvider")
    public static Object[][] dataProvider() {

// Here I want to get the Test Class from which this DataProvider was invoked, so // with that I will supply the test data accordingly.

    Bean oneBean = new Bean();
        oneBean.setfName("Nisarg");
        oneBean.setlName("Dave");
        oneBean.setAddress("India");

        Bean secondBean = new Bean();
        secondBean.setfName("ABCD");
        secondBean.setlName("XYZ");
        secondBean.setAddress("UK");

        return new Object[][] { 
                                    new Object[] { oneBean },
                                    new Object[] { secondBean }
                              };
    }

Here in my DataProvider I know I can pass Method m or ITestContext as parameters, but both of them are not giving me Test class Name from which it was called.

Can anyone please help on this?

  • PLease take time to visit this for better understanding: https://groups.google.com/forum/#!topic/testng-users/crambaqqnJc – nisargdave1993 Dec 23 '19 at 17:35
  • The answer to the issue is: You just need to pass in an ITestNGMethod object as a parameter to your "@DataProvider" method. TestNG will natively inject a TestNG test method object into your data provider via which you should be able to extract out whatever you need. – nisargdave1993 Dec 27 '19 at 06:55
  • Class clazz = method.getRealClass(); Method tm = method.getConstructorOrMethod().getMethod(); Constructor constructor = method.getConstructorOrMethod().getConstructor(); – nisargdave1993 Dec 27 '19 at 06:57

0 Answers0