• 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

Parameterized unit tests

 
Ranch Hand
Posts: 43
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question about Parameterized tests in JUnit.


This is the test, it basically tests if the code throws an exception if you try to make a die with a number of sides that's not allowed (like -1 or greater then the max allowed value).
So I put all those values in the getTestParameters() method, now I also included null as a value, wich seems to me like a valuable test.
Now the problem is that I expect an IllegalArgumentException while running the test and ofcourse null will throw a nullpointerexception.
So my question: Is there a way I can say this test throws either an IllegalArgumentException OR NullPointerException?
Or is there another way to solve this? Do I write a whole new testclass just for the nullvalue?
Hopefully somoene can point me in the right direction,
Thank you very much guys.

Cheerios,
Kenneth
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you expect a different result from a test involving null than a test that is supposed to throw an IllegalArgumentException, then that is different test.
It has different expectations.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, I would question why 'null' is not considered an IllegalArgument.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought you definitely throw an Exception for nulls. I am going to have brickbats thrown at me for saying that Exception shou‍ld be a NullPointerException.

I think this discussion would fit better in our testing forum.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends if the concept is that a null is considered an error or not.
In other words, should this code never be called with a null parameter.

If that's the case then I wouldn't add a test to see what it does for a null.
Checking it throws an NPE when given a null object is a little pointless.

ETA:  Of course, in that case it wouldn't be an illegal argument either!
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok  so what I take from your answers is that as I test for a different expected exception, I should write different test alltogether.
It might not seem to make sense to test for a NPE if I give null as an argument, but as this is a part of a testdriven development project, I should test if it's possibile to make a die with no input for sides.
As this parameter is cliƫntinput.
Null is considered as an error, as are all values in this parameterized test, that was what I was trying to do, put all possible wrong values to the test in one testclass.

Anyhow, thanks for the quick responses!
And thanks for the clarification,

Cheers to all of you,
Kenneth
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Dave Tolls on this: if a null parameter is not legal, it should throw an IllegalArgumentException, not a NPE.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The choice between NPE and IAE can be confusing. I generally do what makes sense but when that distinction is blurry, I go for consistency and least astonishment. The Java API for List consistently documents these two exceptions:

NullPointerException - if the specified element is null and this list does not permit null elements
IllegalArgumentException - if some property of the specified element prevents it from being added to this list

That said, I tend to let nulls cause NPEs rather than IAEs.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a quick question: is the parameter of the Teerling constructor an int or an Integer? Because if it's int, it makes no sense testing with null. If anyone would try that's just an exception during auto-unboxing, and someone is just using your constructor incorrectly.

Right now it's not the test that's causing the NPE, but the JUnit framework (because null is not a valid value for the test constructor) - again because of the auto-unboxing.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rob,
Thanks for the response, it makes a hella lot of sense.
The parameter for the constructor is indeed declared as int rather than Integer.
I don't really know what auto-unboxing is, but I'll be looking into it.

Thanks again,
Cheers to all,
Kenneth
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing is when a wrapped (boxed) Number like Integer can take a primitive like int as its argument.

Without it, you'd have to write

Autounboxing is the same thing in reverse.  You can write

Without it, you'd have to write
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And auto-unboxing a null reference will lead to an NPE. It can be really tricky to find those.

Consider the following example:
That for-loop will get an NPE when the null is unboxed, but the error is reported on the line of the for-loop. Your first guess would be the list, so you'd probably spend quite some time trying to find out what's going wrong here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic