【问题标题】:How to throw exception from Junit test function catch block?如何从 Junit 测试函数 catch 块中抛出异常?
【发布时间】:2016-04-13 09:26:23
【问题描述】:

我的 Junit 测试函数有 try-catch 块。 在 catch 块中,我正在捕获异常以在 ExtentReport 中记录错误。 另外,我正在从 Apache ant 'junitreport' 生成 Junit 报告。 我的问题是,因为我在我的 catch 块中捕获异常,Junit 生成的结果不显示错误。 如何抛出异常(或其他方式)以注册 Junit 结果的异常。

代码如下:

try {
    //Test code
    } catch (Exception e) {
            extentTest.log(LogStatus.ERROR, "Exception in clicking "); //For ExtentREport Logging
            e.printStackTrace();
            throw(new Exception(e.getMessage(), e)); //Expected Junit to capture error, but it is not happening
    }

【问题讨论】:

    标签: junit try-catch


    【解决方案1】:

    您可以重新抛出异常,而不是包装异常。

    try {
        //Test code
    } catch (Exception e) {
        extentTest.log(LogStatus.ERROR, "Exception in clicking "); //For ExtentREport Logging
        e.printStackTrace();
        throw e;
    }
    

    【讨论】:

    • 我曾尝试重新抛出异常。它仍然不会注册为错误。
    猜你喜欢
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多