I am writing tests for several Scalatra routes. I'm using Scala 2.10.2, Scalatra 2.2.1, and Scalatest 1.9.2; I'm fairly new at using all three of these. I'm also using Eclipse Scala IDE 3.0.1 with the ScalaTest plugin.
For my ScalatraBootstrap I have
context.mount(new AccountsController, "/accounts")
context.mount(new ProfilesController, "/profiles")
Inside ProfilesController I have the following route
val addImage =
(apiOperation[Int]("addImage")
parameters (pathParam[String]("context").description("Profile context"),
pathParam[Int]("id").description("Profile Id"),
Parameter("body", "body data",
DataType[ScalatraRecord],
paramType = ParamType.Body)))
post("/:context/:id/images", operation(addImage)) { ... }
This is tested with
test("add a profile image") {
val json = JObject("name" -> JString("asdf")) merge JObject("image_file_url" -> JString("test.png"))
val bytes = compact(render(json)).getBytes("utf-8")
post("/profiles/users/1/images", bytes, Map("Content-Type" -> "application/json")) {
status should be(200)
}
}
This passees the test.
Inside AccountsController I have the following route
val addContact =
(apiOperation[Int]("addContact")
parameters (
Parameter("contact", "new contact data",
DataType[Contact],
paramType = ParamType.Body)))
post("/contacts", operation(addContact)) { ... }
Tested with
test("add contact") {
val json = JObject("name" -> JString(rand.nextString(10))) merge JObject("email" -> JString(rand.nextString(10)))
val bytes = compact(render(json)).getBytes("utf-8")
post("/accounts/contacts", bytes, Map("Content-Type" -> "application/json")) {
status should be(200)
}
}
This fails; the status is 500, and the body is "Not Implemented." The route works fine in production. I tested a get("/accounts/contacts")
route to make sure that I didn't have any typos, and this route also works fine. As far as I can tell, the only difference between the two routes is that the profile route has two path params and uses a different data type for its body param (the test that succeeds uses ScalatraRecord, the test that fails uses Contact; Contact inherits from ScalatraRecord); I tried adding bogus path params to the account route and I also tried changing the type of its body param to match the profile's param, but I ran into the same error.
I don't understand why one route would pass the test while the other route would give me a "Not Implemented" error. Does anybody have any ideas as to how I can fix this?