1

In Scala, I would like to create a TestNG DataProvider that converts all the elements of an enumeration into an Array of Arrays where each element in the outer array is an array containing one of the values of the enumeration.

This is my first attempt, but it returns a Set of Arrays.

@DataProvider(name = "profileIdProvider")
def provideProfiles() = {
  for (profile <- ProfileId.values) yield Array(profile)
}

What I need it to return is something like this:

Array(Array(value1), Array(value2))
2
  • Out of curiosity, why do you need arrays as opposed to Seq or List? The example that you linked to is Java-centric, but in Scala most of what you'd do on an array you can also do on a list. Commented Feb 16, 2012 at 21:13
  • Hi Chris. The TestNG framework requires a DataProvider method to return either an Object[][] or an Iterator<Object[]> Commented Feb 16, 2012 at 21:15

2 Answers 2

4
@DataProvider(name = "profileIdProvider")
def provideProfiles() = {
  ProfileId.values.map(Array(_)).toArray
}

Not tested, but should work I think.

Sign up to request clarification or add additional context in comments.

Comments

2

Something like this should do (modified to use ProfileId.values of course):

def provideProfiles() = { 
    var returnVal = List[Array[Int]]()
    for (profile <- 1 to 5) returnVal :+= Array(profile)
    returnVal.toArray
}

Though I like @missingfaktor's answer more, of course.

1 Comment

Thanks Chris. Your solution works as well. If I needed to preserve ordering, this would be the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.