【问题标题】:JUnit test case for Java Runnable runJava Runnable 运行的 JUnit 测试用例
【发布时间】:2019-04-18 04:17:19
【问题描述】:

即使出现异常,我也希望我的程序能够正常运行。以下是这样做的。有人可以帮我为此编写 JUnit 测试用例吗?

protected static Runnable myMethod=new Runnable() {
    @Override
    public void run() {
        try {
            //my code - may raise exception
        } catch (Throwable t) {
            logger.error("Exception occured", t.getMessage());
        }
    }
};

【问题讨论】:

    标签: java multithreading junit mockito runnable


    【解决方案1】:

    @Test 方法抛出异常将导致它以错误结束,这不是成功的。所以这种情况的教科书方法是设置条件,运行方法,如果异常不会导致测试出错,则假设一切正常:

    @Test
    public void testMyLogic() {
        // Set up conditions that would cause an the Runnable's body to throw an exception
        myMethod.run();
    
        // If we got here an exception was NOT thrown.
        // Implicitly, we're OK.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多