开发者

Can Mockito handle methods that return generics in Scala?

开发者 https://www.devze.com 2023-03-09 02:22 出处:网络
I want to mock the return from javax.servlet.http.HttpServletRequest, getParameterNames(). Therefore:

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 java开发者_开发知识库x.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消