qantik qantik - 2 months ago 20
Scala Question

How to compare String Arrays with JUnit in Scala

I was trying to compare to String Arrays in a JUnit test suite in Scala using

assertArrayEquals
. It works fine for basic types like
Int
or
Boolean
, however it issues the following error when applying it to
String
:


overloaded method value toString with alternatives: (x$1: Array[Object])String (x$1: Array[Double])String (x$1: Array[Float])String (x$1: Array[Boolean])String (x$1: Array[Byte])String (x$1: Array[Char])String (x$1: Array[Short])String (x$1: Array[Int])String (x$1: Array[Long])String cannot be applied to (Array[String])


This seems weird to me since using the same method in a Java environment works without a problem.
Is there a way to circumvent this?

Answer

My "general" answer: one only needs one assert; and that is assertThat. That assert works with Hamcrest matchers, so typically you write down

assertThat(expected, is(whatever))

And using the wide range of existing hamcrest matchers, you immediately get "expected" results; even for collections and such things.

And if the standard means don't work; writing custom matchers is a straight forward task, too.

So, long story short: just use that assert that simply works.