Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG - How to get current class name from BeforeClass

Tags:

java

testng

TestNG - How to get current class name from BeforeClass.

I have class A with extends class B

class B {
    @BeforeClass
    public void beforeClass() {
        /* Need test class name 'A' */
    }
}
class A extends B {
    @Test
    public void test() {
        /* do something */
    }
}

In BeforeClass I have tried the only two parameters which inject: ITestContext and XmlTest. However, no luck in determining how to get the class name from either.

Anyone got any ideas? Thanks

like image 706
mgoodnow Avatar asked Dec 01 '22 06:12

mgoodnow


2 Answers

Following should do the trick:

@BeforeClass
public void beforeClass() {
    String className = this.getClass().getName();
}
like image 57
artdanil Avatar answered Dec 10 '22 22:12

artdanil


How about getClass()?

(a few more characters to keep SO happy)

like image 22
Cedric Beust Avatar answered Dec 10 '22 23:12

Cedric Beust