1 package org.junit.rules; 2 3 import org.junit.runner.Description; 4 5 /** 6 * The TestName Rule makes the current test name available inside test methods: 7 * 8 * <pre> 9 * public class TestNameTest { 10 * @Rule 11 * public TestName name= new TestName(); 12 * 13 * @Test 14 * public void testA() { 15 * assertEquals("testA", name.getMethodName()); 16 * } 17 * 18 * @Test 19 * public void testB() { 20 * assertEquals("testB", name.getMethodName()); 21 * } 22 * } 23 * </pre> 24 */ 25 public class TestName extends TestWatcher { 26 private String fName; 27 28 @Override starting(Description d)29 protected void starting(Description d) { 30 fName= d.getMethodName(); 31 } 32 33 /** 34 * @return the name of the currently-running test method 35 */ getMethodName()36 public String getMethodName() { 37 return fName; 38 } 39 } 40