Question

I am Scala newbie

I have a java method like this below

public void myMethod (Collection<String> param) throws MyException {
    // process param
}

I want to test this method using a Scala Junit test

@Test
def myMethodTest() {
    try {
        ....myMethod(Seq("myString"))
    } catch {
        case e : MyException => throw new AssertionError ("Failed myMethod : " + e.getMessage)
    }
}

But it is giving me a Type mismatch error. How can I fix this?

Was it helpful?

Solution

Seq is not a Java Collection. You should use JavaConverters implicits to convert them:

import scala.collection.JavaConverters._

myMethod(Seq("myString").asJava)

Or you can use Java collections directly, for example, via Arrays.asList():

import java.util.Arrays

myMethod(Arrays.asList("myString"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top