我想在扩展 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
最佳答案
你能覆盖 ParentRunner#classBlock
? 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) { /* ... */ }
关于java - 在 JUnit 4 中,有没有一种方法可以在扩展 BlockJUnit4ClassRunner 的自定义运行程序中的所有 @BeforeClass 之前运行一段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25720758/