How can I get name of the test method in JUnit 5?
Declare a parameter of type TestInfo
in your test method and JUnit will automatically supply an instance of that for the method:
@Test
void getTestInfo(TestInfo testInfo) { // Automatically injected
System.out.println(testInfo.getDisplayName());
System.out.println(testInfo.getTestMethod());
System.out.println(testInfo.getTestClass());
System.out.println(testInfo.getTags());
}
You can get test method name (and more) from the TestInfo
instance as shown above.
Personally I prefer to use this:
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
来源:https://stackoverflow.com/questions/52074872/what-is-the-equivalent-of-testname-rule-in-junit-5