I use below Java code to check if test fails

public void myMethod(){
}catch(AssertionError e){

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

  • 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
    Mar 4, 2016 at 12:39

2 Answers 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.


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){
throw e;


catch(AssertionError e){

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

  • @Anoop Please let me know if this resolves your query. 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 Mar 4, 2016 at 10:18
  • Okay, if you find it doubtful, you can use exisiting testng method in catch block. Assert.fail(); 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
    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 Mar 8, 2016 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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