【发布时间】:2011-04-25 23:09:10
【问题描述】:
当我执行多个 JUnit 测试时,我发现静态块只运行一次。 如何强制它为每种测试方法运行?我使用的是最新的 JUnit 4.8.2
另外,根据 xUnit 的设计原则,每个方法都应该完全独立于其他方法。为什么静态块只执行一次?
@Test TestMethod1 () {
Accounts ac = new Accounts();
ac.method1(); //kill the thread inside
}
@Test TestMethod2 () {
Accounts ac = new Accounts();
ac.method2(); // the thread is no longer available!!
}
class Accounts {
static {
// initalize one thread to monitor something
}
}
这甚至发生在 TestMethod1 和 TestMethod2 位于不同的测试类中时。
【问题讨论】:
-
请展示一些代码来解释你如何运行它
-
如果你想在每个@Test 运行之前初始化一些东西,在@Before 注释方法中进行
标签: java unit-testing junit