I'm working on a Scala url shortener (to be hosted at my awesomely problem-causing ❺➠.ws domain). Since this is such a small application, I'm rolling it ground-up from the Servlet spec only, to get a feel for Scala without having to worry about a lot of dependencies. As such, one of the things I need to do is parse the requestor's "representation", i.e. determine if I'm serving JSON, XML, or HTML. Since this comes from the Accept: header, my tests will need to mock HttpServletRequest.
val enumeration = createMock(
classOf[java.util.Enumeration[String]])
expect(enumeration.hasMoreElements).andReturn(true)
expect(enumeration.nextElement).andReturn("TEXT/HTML")
expect(enumeration.hasMoreElements).andReturn(false)
// the following call doesn't compile!!!
expect(request.getHeaders(parser.ACCEPT_HEADER))
.andReturn(enumeration)
expect(request.getParameter(parser.TYPE_PARAM))
.andReturn(null)
replay(request)
replay(enumeration)
val t = parser.determineRepresentation(request)
t should equal (Some("text/html"))
TestRepresentationParser.scala:21: type mismatch;
found : java.util.Enumeration[String]
required: java.util.Enumeration[?0] where type ?0
EasyMock.expect(request.getHeaders(parser.ACCEPT_HEADER))
.andReturn(enumeration)
I had given up on testing this for a while, but eventually the simple solution prevailed:
EasyMock.expect(request.getHeaders(parser.ACCEPT_HEADER))
// this call is obviously not type-checked, so it works
expectLastCall.andReturn(enumeration)