I want to get the name of currently executing TestCase Method in @Before method. Example
public class SampleTest()
{
@Before
public void setUp()
{
//get name of method here
}
@Test
public void exampleTest()
{
//Some code here.
}
}
As discussed here, try using @Rule and TestName combination.
As per the documentation before method should have test name.
Annotates fields that contain rules. Such a field must be public, not static, and a subtype of TestRule. The Statement passed to the TestRule will run any Before methods, then the Test method, and finally any After methods, throwing an exception if any of these fail
Here is the test case using Junit 4.9
public class JUnitTest {
@Rule public TestName testName = new TestName();
@Before
public void before() {
System.out.println(testName.getMethodName());
}
@Test
public void test() {
System.out.println("test ...");
}
}
Try using a @Rule
annotation with org.junit.rules.TestName
class
@Rule public TestName name = new TestName();
@Test
public void test() {
assertEquals("test", name.getMethodName());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With