0

After executing the below code testng reports shows as pass due to try-catch block, but I need to fail the test if the control come into catch block.

@Test
public void testIt( Method method )
{
    Webdriver d1= new FirefoxDriver();
    d1.get(http://www.google.com);
    String title=d1.getTitle();
    String var=Gogle;
    System.out.println("start execution!!!");
    try {
        Assert.assertEquals( title,var );
    } catch( Throwable er ) {
        System.out.println("Error in method '" + method.getName() + "'.);}
        System.out.println("Execution End!!!");
    }
}
0

2 Answers 2

2

In the catch block call Assert.fail(String message, Throwable cause) with an error message of your choice as the first parameter, and the Throwable er from the catch as the second parameter.

2

Your code is swallowing the exception. You are handling the AssertError and basically just printing out some text rather than allowing TestNG to handle the exception. To fix it, just get rid of the entire try-catch block in your code and just call the .assertEquals on a single line of code.

1
  • If I do not use the try-catch block the code will fail in "Assert.assertEquals( title,var );" line and the flow will not come to the next line "System.out.println("Execution End!!!");", to continue the execution I need to use the try-catch block. So my objective is if some line will fail then the entire flow should be stopped also the test will reported as a failed in the report.
    – Andy
    Jul 23, 2014 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.