I want to get currently executing test method in @Before
so that I can get the annotation applied on currently executing method.
public class TestCaseExample {
@Before
public void setUp() {
// get current method here.
}
@Test
@MyAnnotation("id")
public void someTest {
// code
}
}
try TestName rule
public class TestCaseExample {
@Rule
public TestName testName = new TestName();
@Before
public void setUp() {
Method m = TestCaseExample.class.getMethod(testName.getMethodName());
...
}
...
Evgeniy pointed to the TestName
rule (which i'd never heard of - thanks, Evgeniy!). Rather than using it, i suggest taking it as a model for your own rule which will capture the annotation of interest:
public class TestAnnotation extends TestWatcher {
public MyAnnotation annotation;
@Override
protected void starting(Description d) {
annotation = d.getAnnotation(MyAnnotation.class);
}
}