3

I came across a code written by someone with Assert.fail("some text") in a catch block. This is my code:

try {
  //WebDriver code here which interacts with WebElements
  //When an exception occurs in this try block, it will be caught in 
  //the catch and further catch block has Assert.fail()
} catch (Exception e) {
    Assert.fail("failed to click 'Webelement' ");
}

I somehow felt this is not right way to do it. Am i wrong?

Note: Not exactly a duplicate of Is Assert.Fail() considered bad practice , As i am not expecting any Exception. If any exception occurs i need to fail the test case

Naman
  • 27,789
  • 26
  • 218
  • 353
user2048204
  • 729
  • 4
  • 13
  • 27
  • 1
    Possible duplicate of [Is Assert.Fail() considered bad practice?](https://stackoverflow.com/questions/120648/is-assert-fail-considered-bad-practice) – Ori Marko Sep 03 '17 at 06:29
  • Not a duplicate of [Is Assert.Fail() considered bad practice](https://stackoverflow.com/questions/120648/is-assert-fail-considered-bad-practice) , As i am not expecting any Excpetion. If any exception occurs i need to fail the test case. – user2048204 Sep 03 '17 at 08:49
  • 2
    The worse practice here is suppressing the actual exception (whose message and stack trace may contain insights into the test failure), and replacing it with your own exception. Just declare that the test case `throws Exception` and let the test framework handle it. – Andy Turner Sep 03 '17 at 10:10

1 Answers1

3

Though it's syntactically not incorrect. But you should preferably rephrase your tests to use expectedException instead and be specific about the exception is thrown as well. For e.g. :

If your method fromTest() when called with "test" as an argument could throw an NumberFormatException, then your test definition for such behaviour should be :

@Test(expectedExceptions = NumberFormatException.class)
public void testMethod() {
    System.out.println("About to throw an exception!");
    fromTest("test");
}

Note: If you believe that the exception may so happen within your test execution itself instead of any other method being called from it. I would suggest to not catch it. Let it fail and then you shall fix it.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    Thank you for the quick response. As far as i understand 'expectedException' will allow my test to pass when that exception occurs in the test. But I am not expecting my test to throw any exception and in case any exception occurs my test should Fail. So as i understand, my test method should not have any try-catch blocks and in case a exception occurs, i should let TestNG handle it ( and TestNG fails the test - which should be fine) – user2048204 Sep 03 '17 at 16:27
  • 1
    @user2048204 *But I am not expecting my test to throw any exception and in case any exception occurs my test should Fail.*... If your test results in an unexpected exception, you shouldn't practically catch it. Instead, fix it or let the framework you are using decide on how to handle that. – Naman Sep 03 '17 at 16:33