Win a copy of Head First Android this week in the Android 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JUnit5 Parameterized Test - Using @EnumSource for multiple parameters?

 
Marshal
Posts: 5146
321
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the JUnit5 User Guide it gives an example of how you can use Parameterized Tests to inject every value of an enum into a test.

I would like to know if it's possible, using JUnit5, to inject every value combination of multiple enums into a test. Something like this, for example:

Of course this example won't compile because the @EnumValue annotation doesn't accept multiple values. But hopefully you get the idea of what I'm looking for?
 
Saloon Keeper
Posts: 13394
296
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this will work, but it won't hurt to try:
 
Tim Cooke
Marshal
Posts: 5146
321
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a creative idea but no dice

 
Stephan van Hulst
Saloon Keeper
Posts: 13394
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm surprised they didn't allow the parameters to be annotated directly. Maybe your best bet is to write your own ArgumentsSource annotation that is repeatable, but you might have to look in the source to see how ParameterizedTest is processed first, when it comes to multiple parameters.

I only learned a little bit about JUnit 5 after reading this topic, but the unclear way the parameters are injected, the lack of support for Hamcrest matchers in the Assumptions class, and the removal of the experimental Theories runner make me reluctant to move away from JUnit 4.
 
author & internet detective
Posts: 40797
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
Not with @EnumSource as that is for one enum. I think you are stuck with @MethodSource and actually write the method that returns the combinations. Sorry.
 
Tim Cooke
Marshal
Posts: 5146
321
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a shame for sure but I'm not really surprised. I achieved it using the @MethodSource but it's not as tidy as I'd hoped.

 
Stephan van Hulst
Saloon Keeper
Posts: 13394
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, are you just trying JUnit 5 out, or have you good reasons to use it over JUnit 4? If so, I'd like to know what your considerations are.
 
Tim Cooke
Marshal
Posts: 5146
321
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm mostly exploring but given JUnit 5 has now been fully released it's the most up to date version I intend to use it in real life.

At some point I shall be updating my work project to JUnit5 and hopefully Jeann's JUnit4 to JUnit5 converter will help a long way towards getting that done.
 
Stephan van Hulst
Saloon Keeper
Posts: 13394
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JUnit 4, I would have written your code like this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic