package org.junit.rules;

import org.junit.runner.Description;

The TestName Rule makes the current test name available inside test methods:
public class TestNameTest {
 @Rule
 public TestName name= new TestName();
 @Test
 public void testA() {
     assertEquals("testA", name.getMethodName());
    }
 @Test
 public void testB() {
     assertEquals("testB", name.getMethodName());
    }
}
Since:4.7
/** * The TestName Rule makes the current test name available inside test methods: * * <pre> * public class TestNameTest { * &#064;Rule * public TestName name= new TestName(); * * &#064;Test * public void testA() { * assertEquals(&quot;testA&quot;, name.getMethodName()); * } * * &#064;Test * public void testB() { * assertEquals(&quot;testB&quot;, name.getMethodName()); * } * } * </pre> * * @since 4.7 */
public class TestName extends TestWatcher { private volatile String name; @Override protected void starting(Description d) { name = d.getMethodName(); }
Returns:the name of the currently-running test method
/** * @return the name of the currently-running test method */
public String getMethodName() { return name; } }