0

I use below Java code to check if test fails

@Test
public void myMethod(){
..
..
Assert.assertEquals(condition1,condition2);
}catch(AssertionError e){
e.printStackTrace();
}

Even if condition1 and condition2 are not equal, testNG showing

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

in the eclipse console. In my understanding the test should fail and failure count should be 1

1
  • 2
    In the code above, you catch the AssertionError in order to print the stack trace. Your test runner will print the stack trace for you, and the test runner uses the AssertionError to determine that the test failed, so in your example, you should simply remove the catch(AssertionError){}.
    – user246
    Commented Mar 4, 2016 at 12:39

2 Answers 2

2

Just stop catching AssertionError exceptions and you will see your tests failing as expected.

Why this so? When an assertion evaluates to false:

assertEquals(3, 5);
assertThat(3, is(equalTo(5)));
assert 3 == 5;

in all those cases it throws AssertionError exception. Any exception thrown in the test terminates the test, unless it is caught. So in your case, if you catch the exception, then your test is not terminated and considered as passed. A test execution framework (TestNG, JUnit, etc.) has simply no knowledge that any assertion failed.

However, if your test throws an exception, whether it is AssertionError or other exception, the test execution framework will consider your test as failed (with one small exception to SkipException that the test execution framework will mark your test as skipped).

You haven't explained why you catch AssertionError in your test. But if you care about printing the stacktrace of AssertionError, your test execution framework will do it for you automatically after failing the test. So printing it explicitly in your test is not necessary.

0
3

Its because you are catching the exception, your assertion is surrounded with try catch block, so your testcase gets pass.

Update your catch block as below.

catch(AssertionError e){
e.printStackTrace();
throw e;
}

OR

catch(AssertionError e){
    e.printStackTrace();
    Assert.fail();
    }

This will throw the exception and your testcase will be failed.

6
  • @Anoop Please let me know if this resolves your query. Commented Mar 4, 2016 at 10:02
  • This is a solution, but I doubt if is it a good practise to throw the caught exception from catch block Commented Mar 4, 2016 at 10:18
  • Okay, if you find it doubtful, you can use exisiting testng method in catch block. Assert.fail(); Commented Mar 4, 2016 at 10:28
  • @Anoop It's a bad practice to swallow exception that should terminate your test, as you did in your question. What is the reason you're catching AssertionError and printing it? If there's AssertionError in your test it will be printed any way after test execution.
    – dzieciou
    Commented Mar 4, 2016 at 11:39
  • After each test is executed I write the test result int to an excel file. By catching the exception I can determine whether the test is passed or failed @dzieciou Commented Mar 8, 2016 at 3:45

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.