Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JUnit5 Parameterized Test - Using @EnumSource for multiple parameters?  RSS feed

 
Tim Cooke
Marshal
Posts: 3838
221
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7711
141
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this will work, but it won't hurt to try:
 
Tim Cooke
Marshal
Posts: 3838
221
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a creative idea but no dice

 
Stephan van Hulst
Saloon Keeper
Posts: 7711
141
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 3838
221
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 7711
141
  • Mark post as helpful
  • send pies
  • 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: 3838
221
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 7711
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JUnit 4, I would have written your code like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!