Reputation: 9
Below code failed to report an error:
@Test
public void test() {
int a = 12;
int b =20;
try {
assertEquals(a, b);
}catch(Throwable t){
}
The code ran successfully and did not report any error. However, it was suppose to report an error as a!=b . If I don't use try catch block then it did report an error. Don't know why assertion is not working in try catch block. Any help will be appreciated. Thanks
Upvotes: 0
Views: 1807
Reputation: 3384
That is happening because you are using
catch(Throwable t){
}
Instead, if you use
catch(Exception e){
}
Your code should work fine.
Check the explanation here : Difference between using Throwable and Exception in a try catch
Upvotes: 0
Reputation: 910
Assertions in Java are built on top of Java exceptions and exception handling. When a Java assertion fails, the result is an AssertionError exception that can be caught like any other Java exception. Hence, when you put assertion in try-catch block and catch the exception thrown by assertion, the execution continues. Please refer to link for more details about Java Assertions and Exceptions. Let me know, if you have any further queries.
Upvotes: 1