gladed
gladed

Reputation: 1743

Can Mockito handle methods that return generics in Scala?

I want to mock the return from javax.servlet.http.HttpServletRequest, getParameterNames(). Therefore:

import org.specs.Specification
import org.specs.mock.Mockito
import scala.collection.JavaConversions._
import javax.servlet.http.HttpServletRequest

object SomethingSpec extends Specification with Mockito {
    "Something" should {
        "do something" in {
            val request = mock[HttpServletRequest]

            // This is fine
            val elements: java.util.Enumeration[String] = List("p1", "p2").iterator

            // But this bombs
            request.getParameterNames() return elements
        }
    }
}

Compilation of the last line results in this difficult-to-understand error:

found   : java.util.Enumeration[String]
required: java.util.Enumeration[?0] where type ?0

Am I doing something wrong?

Upvotes: 1

Views: 1056

Answers (1)

Steve
Steve

Reputation: 19570

have you tried to cast the return value from the HttpServletRequest like

request.getParameterNames().asInstanceOf[java.util.Enumeration[String]] returns elements

It seems, getParameterNames returns an untyped Enumeration.

Upvotes: 1

Related Questions