Assertion error (TestNG) catching issue using java

Prabu picture Prabu · Jul 24, 2013 · Viewed 8.9k times · Source

please find the code

Test(dataProvider = "User_login")
public void StatusForm_Verification(String uname, String pwd)
    throws InterruptedException {
    try {
NavigateToLogin();
Dashboard RD = LoginAs_user(uname, pwd);
Thread.sleep(2000);

    if (Integer.parseInt(ReviewedStatuscount) >= 1) {

        Assert.assertEquals("true",
                revui.Btn_SaveReview.getAttribute("disabled"));

        Assert.assertEquals("true",
                revui.Btn_submitReview.getAttribute("disabled"));

        Assert.assertEquals("true",
                revui.Btn_Needmoreinfo.getAttribute("disabled"));

        status = TestLinkAPIResults.PASSED;     

    } else {
        throw new SkipException(
                "Test Skipping - Reviewed count is Zero");
    }
    }catch((AssertionError ex) {
        testlink_result = TestLinkAPIResults.TEST_FAILED;
        msg = ex.getMessage();
}
}

while executed a above method if any Assertion Error thrown then in the TestNG report it should be an FAIL status - this is for correct

but if i use an Try catch block (catching the AssertionError) in the above method the TestNG report was always PASS status, why? How?

Answer

Joel McCance picture Joel McCance · Jul 26, 2013

I'm not familiar with TestLink, but under normal circumstances the only way that TestNG can know whether your test failed is to catch a thrown exception. When you catch the exception here you "swallow" it, preventing it from ever reaching the TestNG framework.

To fix this, just rethrow the exception after doing your TestLink processing, like so:

} catch (AssertionError ex) {
    testlink_result = TestLinkAPIResults.TEST_FAILED;
    msg = ex.getMessage();

    // Rethrow the AssertionError to signal to TestNG that the test failed.
    throw ex;
}