1 package org.junit.tests.experimental.rules; 2 3 import static org.junit.Assert.assertEquals; 4 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Rule; 8 import org.junit.Test; 9 import org.junit.experimental.runners.Enclosed; 10 import org.junit.rules.TestName; 11 import org.junit.runner.RunWith; 12 13 @RunWith(Enclosed.class) 14 public class NameRulesTest { 15 public static class TestNames { 16 @Rule 17 public TestName name = new TestName(); 18 19 @Test 20 public void testA() { 21 assertEquals("testA", name.getMethodName()); 22 } 23 24 @Test 25 public void testB() { 26 assertEquals("testB", name.getMethodName()); 27 } 28 } 29 30 public static class BeforeAndAfterTest { 31 @Rule 32 public TestName name = new TestName(); 33 34 private final String expectedName = "x"; 35 36 @Before 37 public void setUp() { 38 assertEquals(expectedName, name.getMethodName()); 39 } 40 41 @Test 42 public void x() { 43 assertEquals(expectedName, name.getMethodName()); 44 } 45 46 @After 47 public void tearDown() { 48 assertEquals(expectedName, name.getMethodName()); 49 } 50 } 51 }