001 package org.junit.rules; 002 003 import org.junit.runner.Description; 004 005 /** 006 * The TestName Rule makes the current test name available inside test methods: 007 * 008 * <pre> 009 * public class TestNameTest { 010 * @Rule 011 * public TestName name= new TestName(); 012 * 013 * @Test 014 * public void testA() { 015 * assertEquals("testA", name.getMethodName()); 016 * } 017 * 018 * @Test 019 * public void testB() { 020 * assertEquals("testB", name.getMethodName()); 021 * } 022 * } 023 * </pre> 024 * 025 * @since 4.7 026 */ 027 public class TestName extends TestWatcher { 028 private String name; 029 030 @Override 031 protected void starting(Description d) { 032 name = d.getMethodName(); 033 } 034 035 /** 036 * @return the name of the currently-running test method 037 */ 038 public String getMethodName() { 039 return name; 040 } 041 }