spec2测试失败了scalatra应用程序与swagger支持

时间:2013-02-21 22:56:35

标签: scala specs2 scalatra swagger

在scagtra app上调用原型spec3测试用例时,测试失败。

这是测试/规范代码: 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"""
  }  
}

TestController看起来像:     包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测试失败,出现以下错误(没有详细的跟踪):     [错误] x应该返回成功     [错误]'     [错误]     [错误]     [错误]错误500 com.acme.TestController     [错误]     [错误]     [错误]

HTTP错误:500

    [错误]

访问/ api / account时出现问题。原因:     [错误]

    com.acme.TestController

    [错误]
由Jetty提供://     [错误]
    [错误]     [错误]     [错误]'     [错误]不等于     ...

1 个答案:

答案 0 :(得分:4)

解决方案是更改spec2行:

addServlet(classOf[TestController], "api/*")

为:

implicit val swagger = new GPNSwagger 
addServlet(new TestController, "/api/*")

Swagger注释导致控制器类是抽象的,无法实例化为声明的类型。这种映射servlet的替代方法可以正常工作