JUnit 4:如何在规则中获取测试名称?

时间:2020-01-04 07:38:25

标签: java junit junit4 junit-rule

JUnit 4:如何在Rule中获取测试名称?例如,

public class MyRule extends ExternalResource {

    @Before
    public void before() {
        // how to get the test method name to be run?
    }
}

1 个答案:

答案 0 :(得分:2)

如果您只需要带有测试名称的@Rule,就不必重新安装轮子,只需使用内置的TestName @Rule

如果您试图建立自己的规则以增加一些逻辑,请考虑对其进行扩展。如果这也不是一个选择,则可以将其复制为implementation

要像其他TestRule一样在评论中回答问题,{​​{1}}也有一种ExternalResouce方法。您可以通过覆盖它来添加功能,只需确保调用super方法即可,这样就不会破坏apply(Statement, Description)功能:

ExternalResource