首页 文章

如何从Junit测试函数catch块中抛出异常?

提问于
浏览
0

我的Junit Test函数有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
    }

Screen shot of both Extent Report and Junit report of the same test

1 回答

  • 0

    而不是包装异常,你可以重新抛出它 .

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

相关问题