• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling in Junit

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1) Is it good proactise to put assert statement in catch block?

For example:

I am testing method ABC which returns me int value.If this method executes successfully it return any postive number greated than zero but if is not successfull it return
0.



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) ------------------------2.I want to
know why it is not printing this staement assertTrue("Method not executed sucessfully",false)-----------------------------1



 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic