1

I want to test a particular system behavior considering all the values (and at times excluding a few) from an enum. This can be easily achieved in Junit5 using the @EnumSource annotation. Is there any alternative in Spock2 (or any simple workaround)?

Saikat
  • 14,222
  • 20
  • 104
  • 125

1 Answers1

2

You can use Spocks ability to consume any Iterable as data source:

import spock.lang.*

class EnumSpec extends Specification {
  def "let's try this!"(Color color) {
    expect:
    color.name() == ''

    where:
    color << Color.values()
  }
}

enum Color {
 RED, BLUE, GREEN
}

This test will of course fail three times, as the name is not empty, but it shows how you can iterate over all enum values.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66