正如this answer的第一条评论所述,使用java.lang.ClassLoader.setDefaultAssertionStatus(boolean)
只能为新加载的类启用断言。
有没有办法在已加载的类上启用断言?
我用来验证这个的单元测试是以下(JUnit)
@Test(expected = AssertionError.class)
public void testAssertsEnabled() {
assert false;
log.info("asserts were not enabled. Trying to enable them");
enableAsserts(); //setDefaultAssertionStatus doesn't work since this class was already loaded
assert false;
}