• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is this an acceptable way of writing JUnits or a simple a bad practice ?

 
Ranch Hand
Posts: 2599
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In some JUnit codes I have seen that if one has to test some method, instead of actually testing by asserting that the method returns the expected out , one simply does like below :


Seeing such code, I feel although it doesn't test the actual thing, it partially serves the purpose.

Is this acceptable practice or simply a bad practice ?

Thanks.


 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I regard that as a bad practice, especially if the exception is ignored. With this code, the test fails without any reason. If the exception is simply thrown (so without the try-catch), the test will fail with an error, and the exception will be available.

The other way around is more interesting:

I've used this quite a lot in the past. However, since JUnit 4.13 and JUnit 5, this is no longer needed. Instead, assertThrows can be used:

There are some warnings about that though - the code inside assertThrows should preferably have only one method call. That way, you know which method triggers the exception. (I've had Sonar complain many times if I used more than one method call).
 
Saloon Keeper
Posts: 25483
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, anything that throws away an Exception message when it doesn't even know what the Exception is is VERY bad practice regardless of whether it's in test code or mainline code. And those who do it where I can catch them should be prepared to face dire consequences.
 
Monica Shiralkar
Ranch Hand
Posts: 2599
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, anything that throws away an Exception message when it doesn't even know what the Exception is is VERY bad practice



This is first reason. I think the second reason for it to be bad code is that just absence of exception thrown doesnt mean code is working fine.
 
Rob Spoor
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very true.
 
I'm still in control here. LOOK at this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic