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