aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/TestName.java
blob: bf726023bf681a15ffd7c327927da8763176284f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.junit.rules;

import org.junit.runner.Description;

/**
 * The TestName Rule makes the current test name available inside test methods:
 *
 * <pre>
 * public class TestNameTest {
 *  &#064;Rule
 *  public TestName name= new TestName();
 *
 *  &#064;Test
 *  public void testA() {
 *      assertEquals(&quot;testA&quot;, name.getMethodName());
 *     }
 *
 *  &#064;Test
 *  public void testB() {
 *      assertEquals(&quot;testB&quot;, name.getMethodName());
 *     }
 * }
 * </pre>
 *
 * @since 4.7
 */
public class TestName extends TestWatcher {
    private String name;

    @Override
    protected void starting(Description d) {
        name = d.getMethodName();
    }

    /**
     * @return the name of the currently-running test method
     */
    public String getMethodName() {
        return name;
    }
}