Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Exception handling in Junit

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Fire me boy! Cool, soothing, shameless self promotion:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic