2

I needed to get a class name from my calling class. I will not make too much confusion.

I have 3 classes .

Class A{ //do something } 

Class B extends Class A 
{ 
     @Test(dataProvider = "dp" , dataProviderClass = C.class)
     public void methodone(String name)
     {
       //do something
     }
} 


Class C extends Class A 
{
    @DataProvider(name="dp")
    public Object[][] getData(Method m) {
        String sheetName = m.getName();
        System.out.println("---"+sheetName); }
} 

In the Class C, i need the name of the calling class to be printed . But for now it is printing the name of the calling method which is "methodone" of class B. But i need as "class B" to be printed. Can some one help. Thank you.

3

This will help you

public class Dummy {
    @DataProvider(name="dp")
    public Object[][] getData(Method m) {
        //this will print respective class name
        System.out.println(m.getDeclaringClass());
        return new Object[][] {{"name"}};
    }
}

Will print the class name as shown in image below enter image description here

You can get only the class name by applying the split feature over the extracted string

Using split function to get the class name

@DataProvider(name="dp")
    public Object[][] getData(Method m) {
        String classname=m.getDeclaringClass().toString().split(" ")[1];
        System.out.println(classname.split("\\.")[classname.split("\\.").length-1]);
        return new Object[][] {{"Testing is awesome"}};
    }

Using inbuilt function to get the class name

@DataProvider(name="dp")
    public Object[][] getData(Method m) {
        String classname=m.getDeclaringClass().getSimpleName();
        System.out.println(classname);
        return new Object[][] {{"Testing is awesome"}};
    }
| improve this answer | |
  • 1
    You can just use ..getDeclaringClass().getSimpleName() instead of splitting everything.. – Alexey R. Jul 22 at 10:48
  • 1
    You can accept it by clicking on the check mark near the answer – Mohamed Sulaimaan Sheriff Jul 22 at 10:50
  • 2
    @AlexeyR. Updated the answer – Mohamed Sulaimaan Sheriff Jul 22 at 10:57
  • 2
    @user12730187 accepting the answers will help others to find the solutions more effectively. It also gives you reputation points that makes you obtain more capabilities on the forum. – Alexey R. Jul 22 at 11:02
  • 1
    Hi Mohammed and Alexey , Apologies . For a brief moment i had forgotten how to accept the answer. i have done it . Many thanks to you both. – user12730187 Jul 22 at 11:09
0

Just to add to the above answer,

If you refer the documentation

https://testng.org/doc/documentation-main.html#parameters-dataproviders

If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, 

so whatever class methods supported for java.lang.reflect.Method class will be supported inside the data provider, you can see all the available methods at:

https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.