Java 在JUnit4中,是否有一种方法可以在扩展BlockJUnit4ClassRunner的自定义运行程序中的all@BeforeClass之前运行一段代码

Java 在JUnit4中,是否有一种方法可以在扩展BlockJUnit4ClassRunner的自定义运行程序中的all@BeforeClass之前运行一段代码,java,junit,junit4,Java,Junit,Junit4,在扩展BlockJUnit4ClassRunner的自定义运行程序中调用@BeforeClass之前,我想运行一些方法 我该怎么做 当重写runChild方法时,我发现它按测试方法运行 protected void runChild(FrameworkMethod child, RunNotifier notifier) { System.out.println("a"); super.runChild(child, notifier); System.out.print

在扩展
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
你能超越吗?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) { /* ... */ }