Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spec2 tests failing for scalatra app with swagger support

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]

HTTP ERROR: 500

[error]

Problem accessing /api/account. Reason: [error]

    com.acme.TestController
[error]
Powered by Jetty:// [error]
[error] [error] [error] ' [error] is not equal to ...
like image 495
drsquidop Avatar asked Dec 09 '25 00:12

drsquidop


1 Answers

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

like image 55
drsquidop Avatar answered Dec 11 '25 21:12

drsquidop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!