以编程方式在已加载的类上启用断言

时间:2015-07-09 18:25:55

标签: java junit assert assertions

正如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;
}

0 个答案:

没有答案