Get test class name from which data provider method is called in TestNG

Skip to first unread message

Nisarg Dave

Dec 24, 2019, 4:38:33 AM12/24/19
to testng-dev

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() {

        Bean oneBean = new Bean();

        Bean secondBean = new Bean();

        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.

The order in which the execution will run (for annotated methods) in TestNG is: 


And, one more thing is: I am not applying dataProvider attribute in each @Test test case, because I am looking to run one entire flow in particular sequence:

Lets consider an example of Facebook application: Sign up --> Login --> Send a friend Request --> Logout. These 4 are functionalities (in short 4 @Test in a Test Class)

So if I pass 3 dataproviders to each @Test method, then they will get executed in below order:

Sign up --> Sign Up --> Sign Up
Login --> Login --> Login
Send a friend Request --> Send a friend Request --> Send a friend Request
Logout --> Logout --> Logout

However, I want to run in in below Sequence for 3 different Data providers:

Sign up --> Login --> Send a friend Request --> Logout
Sign up --> Login --> Send a friend Request --> Logout
Sign up --> Login --> Send a friend Request --> Logout

So, I am using @Factory annotation. So, now the issue is in my DataProvider method, I want to know, from which test class, this Data provider method was invoked, so I will provide the data accordingly.

I know I can add parameters in dataprovider method as: Method m and ITestContext. But as DataProvider is supplied to @Factory method, Method m comes null & ITestContext also doesn't have class related info from which it was called. 

Can anyone please help on this?

⇜Krishnan Mahadevan⇝

Dec 25, 2019, 9:37:23 PM12/25/19
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.

Here's a sample

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.ITestNGMethod;
import org.testng.annotations.DataProvider;

public class DataProviderHouse {

@DataProvider(name = "dataProvider")
public static Object[][] dataProvider(ITestNGMethod method) {
Class clazz = method.getRealClass();
Method tm = method.getConstructorOrMethod().getMethod();
Constructor constructor = method.getConstructorOrMethod().getConstructor();
String name = clazz.getName() + ".";
if (tm == null) {
name = constructor.getName();
} else {
name = name + tm.getName();

System.err.println("Data provider is being invoked by " + name);
Bean oneBean = new Bean("Master Oogway", 75);
Bean secondBean = new Bean("Master Shifu", 60);

return new Object[][] {new Object[] {oneBean}, new Object[] {secondBean}};
public class Bean {

private final String name;
private final int age;

public Bean(String name, int age) { = name;
this.age = age;

public String getName() {
return name;

public int getAge() {
return age;

import org.testng.Assert;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestClass {

private final String name;
private final int age;

@Factory(dataProvider = "dataProvider", dataProviderClass = DataProviderHouse.class)
public TestClass(Bean bean) { = bean.getName();
this.age = bean.getAge();

public void testName() {

public void testAge() {
Assert.assertTrue(age > 0 && age <= 100);

public String toString() {
return "TestClass{" + "name='" + name + '\'' + ", age=" + age + '}';

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @
My Technical Scribblings @

You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Nisarg Dave

Dec 26, 2019, 10:04:33 AM12/26/19
to testng-dev
Hey Krishnan,

You know, you are awesome ?

Please do know that you have helped me to solve an issue in which I was stuck by a week or so. Thanks a ton !

Because of you guys, the community believes in to hack around TestNG!
Thanks very much !


Reply all
Reply to author
0 new messages