When calling a prototypical spec3 test case on a scalatra app with swagger support, the tests fail.
Here's the test/spec code: ServletSpec.scala
class ServletSpec extends ScalatraSpec { def is =
"Calling the generated swagger client" ^
"should return success" ! swaggerClient^
end
addServlet(classOf[TestController], "/api/*")
def swaggerClient = get("/api/account") {
status must_== 200
response.body must_==
"""my json response"""
}
}
The TestController looks like: package com.newco
import org.scalatra._
import org.scalatra.swagger._
//sample - see http://www.scalatra.org/guides/swagger.html
// JSON-related libraries
import org.json4s.{DefaultFormats, Formats}
// JSON handling support from Scalatra
import org.scalatra.json._
class TestController(implicit val swagger: Swagger) extends ScalatraServlet
with JacksonJsonSupport with JValueResult {
protected val applicationName = Some("AppName")
protected val applicationDescription = "description."
// Sets up automatic case class to JSON output serialization
protected implicit val jsonFormats: Formats = DefaultFormats
// Before every action runs, set the content type to be in JSON format.
before() {
contentType = formats("json")
}
val getAccount =
(apiOperation[GetAccountResponse]("getAccount")
summary "Get users account information"
notes "Returns the users profile"
parameter queryParam[AccessToken]("accessToken").description("Access token returned from authentication service")
)
get("/account", operation(getAccount)){
SampleData.getAccountResponse
}
}
sbt test fails with the following error (no detailed trace available): [error] x should return success [error] ' [error] [error] [error] Error 500 com.acme.TestController [error] [error] [error]
Problem accessing /api/account. Reason: [error]
com.acme.TestController[error]
The solution is to change the spec2 line from:
addServlet(classOf[TestController], "api/*")
to:
implicit val swagger = new GPNSwagger
addServlet(new TestController, "/api/*")
Swagger annotation causes the controller class to be abstract and cannot be instantiated as the declared type. This alternate method of mapping the servlet works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With