Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 2562
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: 22584
122
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: 24889
174
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: 2562
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: 22584
122
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic