0

We're starting to use Scala Test to test our Java application, and I want to test the contents of a Java Collection. We came up with 2 possibilities:

JavaConversions.collectionAsScalaIterable(getJavaCollection()) must contain(allOf(item1, item2).inOrder)

or

Seq(getJavaCollection()).flatten mustEqual Seq(item1, item2)

Being a beginner to Scala, I'm wondering which way would be better (or is there a better way)?

Bernie
  • 2,253
  • 1
  • 16
  • 18

1 Answers1

2
import scala.collection.JavaConverters._
val col = getJavaCollection().asScala //a scala mutable Buffer
col mustEqual Seq(item1, item2)

You can use col as a Seq and perform necessary calculations.

Sometimes it is best to google :)

  1. How can I convert a Java Iterable to a Scala Iterable?
  2. How to convert a java.util.List to a Scala list
  3. Iterating over Java collections in Scala
  4. Converting a Java collection into a Scala collection
Community
  • 1
  • 1
Jatin
  • 31,116
  • 15
  • 98
  • 163
  • If I use mustEqual as in your answer, I get `Wrappers(item1, item2) is not equal to List(item1, item2)`. However, I can do `getJavaCollection().asScala must contain(allOf(item1, item2).inOrder)` and it passes. Thanks for the pointer to JavaConverters. – Bernie Aug 23 '13 at 02:36