我提出了三个可能的(简单的?)修复,在快速测试后对我有用(但您可能需要检查使用静态初始化块的副作用)
1.) 向那些依赖于启用断言的测试用例添加一个静态初始化块
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");
}
}