Mocking Enumeration in Scala with EasyMock
October 21, 2009
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(
// the following call doesn't compile!!!
val t = parser.determineRepresentation(request)
t should equal (Some("text/html"))
When I compile this test, I get the following baffling error message:
TestRepresentationParser.scala:21: type mismatch;
found : java.util.Enumeration[String]
required: java.util.Enumeration[?0] where type ?0
Um, OK? I tried zillions of ways to cast things, even creating my own implementation of Enumeration[String], to no avail. There seems to be
some problem with the fact that HttpServletRequest returns a non-parameterized Enumeration in its interface, but Scala won't
let me create such a thing.
I had given up on testing this for a while, but eventually the simple solution prevailed:
// this call is obviously not type-checked, so it works
Kinda cheesy, and I kinda feel stupid for not thinking of it sooner.