【问题标题】:How to catch java assertion error during runtime如何在运行时捕获 java 断言错误
【发布时间】:2013-10-30 18:53:35
【问题描述】:

前提条件:VM 参数 -ea 已启用

源代码示例:

assert booleanVariable;

如果 booleanVariable 为 false,则会抛出 AssertionError。

我编写了导致 FALSE booleanVariable 的 JUnit 测试,因此如何在不使用插件的情况下捕获错误而不是暂停 JUnit 测试。请注意,该应用程序用作线程模块。因此,JUnit 测试调用一个通用的 postMessage 方法,如下所示:

@Test
    public void myTest(){
        Message invalidMessage = new Message("I am invalid");
        //somewhere in threadedModule source code would throw AssertionError 
        //after reading from its inputQueue
        threadedModule.postMessage(invalidMessage );
        assertNotNull(onputQueue.waitForNextMessage(timeOutTime,timeOutMessage));
}

谢谢!

【问题讨论】:

    标签: exception junit runtime-error assert assertion


    【解决方案1】:

    这无法实现,因为您创建的线程模块在与 JUnit 测试线程不同的线程上运行。 try catch 块只能在自己的线程内捕获 throwable。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-04
      • 2021-01-24
      • 1970-01-01
      • 2012-04-15
      • 2011-02-04
      • 1970-01-01
      • 2020-06-20
      • 2023-03-08
      相关资源
      最近更新 更多