Implementing java.util.Enumeration in scala

52 views
Skip to first unread message

Емил Иванов / Emil Ivanov

unread,
Aug 28, 2009, 5:10:14 AM8/28/09
to specs...@googlegroups.com
Good day to all,

I'm trying to mock an HttpServletRequest for a test using specs and
mockito. Everything works file until the part where I get to mock
getHeaders().

HttpServletRequest.getHeaders(String name) returns a java.util.Enumeration.

I tried implementing java.util.Enumeration using an anonymous class like so:

val raw = mock[HttpServletRequest]
   raw.getHeaders("Accept-Language") returns(
     new java.util.Enumeration[String] {
       val headers = Array("en-us", "bg;q=0.9")
       private var current = 0

       def nextElement: String = {
         headers(current)
       }

       def hasMoreElements: Boolean = {
         current < headers.length - 1
       }
     })

but when compiling I get the following error:

[error]  found   : java.lang.Object with java.util.Enumeration[String]{ ... }
[error]  required: java.util.Enumeration[?0] where type ?0
[error]       new java.util.Enumeration[String] {
[error]       ^

Casting it with asInstanceOf[java.util.Enumeration[String]] doesn't work either.

Hope the question is clear.

Regards,
Emil Ivanov


--
My place to share my ideas:
http://bolddream.com (now live :)

etorreborre

unread,
Aug 28, 2009, 7:47:31 AM8/28/09
to specs-users
I think I already had this issue before when dealing with some pre-
java 5 code.

Did you try to cast things like this:

h.getHeaders("Accept-Language").asInstanceOf[java.util.Enumeration
[String]] returns ......

It is working for me.

Eric.

On Aug 28, 7:10 pm, Емил Иванов / Emil Ivanov <emil.vla...@gmail.com>
wrote:

Emil Ivanov

unread,
Aug 28, 2009, 10:48:39 AM8/28/09
to specs-users
Thank you etorreborre, that fixed it. :)

--
Best,
Emil
Reply all
Reply to author
Forward
0 new messages