amolpalekar kadolkar wrote:Q1) Is it good proactise to put assert statement in catch block?
Could be good practice if you want to test your object state after it threw an exception. Usually you want your test to fail when an exception is thrown, so you just let the exception be thrown (no try/catch block). The test will automatically fail. In some cases you want to test for a specific exception, so you catch that specific exception and tag all other exceptions with failure. For example:
If you are using
junit 4 (and
you should), you can use the expected attribute like this: @Test(expected = ExpectedException.class).
amolpalekar kadolkar wrote:
Q1) To test above method i am purposely entring wrong values.In that case, Once control return from Method ABC() back to this line int s=ref.ABC();
it immediately throws error and print statement in catch block assertTrue("Method not executed sucessfully",false)
check your ABC method. it is probably throwing an exception.
For your information, instead of checking failures like this:
do this:
Makes your code more clear.
Mathieu
http://simpleadn.blogspot.com