有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JUnit 4中的java,有没有一种方法可以在扩展BlockJUnit4ClassRunner的自定义运行程序中,在all@BeforeClass之前运行一段代码

在扩展BlockJUnit4ClassRunner的自定义运行程序中调用@BeforeClass之前,我想运行一些方法

我该怎么做

重写runChild方法时,我发现它按测试方法运行

protected void runChild(FrameworkMethod child, RunNotifier notifier) {
    System.out.println("a");
    super.runChild(child, notifier);
    System.out.println("b");
}

产生以下输出:

a
test here
b
a
test here
b

共 (1) 个答案

  1. # 1 楼答案

    你能覆盖^{}吗?BlockJUnit4ClassRunner扩展ParentRunner,但不覆盖classBlock(或将其标记为final),因此您仍有权覆盖它

    /**
     * Constructs a {@code Statement} to run all of the tests in the test class.
     * Override to add pre-/post-processing.
     * [snip]
     */
    protected Statement classBlock(final RunNotifier notifier) { /* ... */ }