This question already has an answer here:
I want to do some logging while executing my JUnit test. In JUnit 3.x it was always easy to obtain the name of the currently running test case, no matter how the test case was instantiated:
public void testFoo() throws Exception() {
String testName = this.getName();
// [...] do some stuff
}
In JUnit 4 things seem to be not so easy. Does anyone know a solution to this? Is there any option to reflect into the current Runner instance?
What's wrong with:
?
In JUnit 4.7, you can also get the name of the currently executed thest method. May be nice when logging.
Taken from JUnit 4.7 Release Notes (read them here at github) :
OK. I've found another approach [somewhere on the Internet](http://www.nabble.com/What-happened-to-getName()--td23456371.html):
I know this is old, but here is a useful (non-junit) method that I put at the top of all my tests.