我提出了三种可能的(简单吗?)修复它为我工作快速测试后(但你可能需要检查使用静态初始化块的副作用)
1)添加静态-initializer块那些依靠断言测试用例被启用
import ....
public class TestXX....
...
static {
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
}
...
@Test(expected=AssertionError.class)
...
...
2)创建一个基类,所有测试类的扩展这就需要断言启用
public class AssertionBaseTest {
static {
//static block gets inherited too
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
}
}
3)创建它运行所有测试
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
//list of comma-separated classes
/*Foo.class,
Bar.class*/
})
public class AssertionTestSuite {
static {
//should run before the test classes are loaded
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("AssertionTestSuite");
}
}
只是一个一般性的问题时。如果你真的想彻底的单元测试你的代码,你不应该测试启用和不启用断言吗?有一件事断言是**非常好**,因为引入了意想不到的副作用。 – 2009-11-25 16:33:59
btw。你如何运行JUnit测试用例?如果你有一些build manager(ant,maven),默认添加这个开关应该是微不足道的。 – jitter 2009-11-25 16:41:45
@Alexander:这不是一个坏主意。 @jitter:当然你是对的。 Maven似乎默认启用断言。在此之前,我没有注意到Eclipse有一个偏好设置。问题是这些设置可以改变你的背后...... – 2009-11-25 17:03:52