Scalatrasuite测试总是在sbt中失败,在IDE中运行测试时会成功

时间:2013-05-23 12:21:49

标签: scala jetty scalatest scalatra

我在使用ScalaTest进行ScalatraSuite单元测试时遇到了一些问题。

我正在使用的版本: Scalatra:2.2.1 ScalaTest:1.9.1 ScalatraScalatest:2.2.1 码头:9.0.2.v20130417 斯卡拉:2.10.1 SBT:0.12.3

我不得不扩展ScalatraSuite并覆盖baseUrl方法,因为Jetty v9 +我假设有一些代码更改,其中getHost和getLocalPort已被重新分解,因此我在此期间使用它作为临时解决方法:

    trait TempScalatraSuite extends ScalatraSuite {

  override def baseUrl: String =
    server.getConnectors.headOption match {
      case Some(conn) =>
        val networkConn = conn.asInstanceOf[org.eclipse.jetty.server.NetworkConnector]
        val host = Option(networkConn.getHost) getOrElse "localhost"
        val port = networkConn.getLocalPort
        require(port > 0, "The detected local port is < 1, that's not allowed")
        "http://%s:%d".format(host, port)
      case None =>
        sys.error("can't calculate base URL: no connector")
    }
}

现在到了这一点,当在sbt中执行单元测试时,所有scalatra测试都失败了,我尝试运行(通过sbt)而没有运气的测试示例:

 class MyTestServiceHttpServiceSpec extends TempScalatraSuite with WordSpec {

  val log = LoggerFactory.getLogger(this.getClass.getName)

  addServlet(classOf[MyTestServiceHttpService], "/*")

  val contentType = Map("Content-Type" -> "application/json")

  "An invocation to /subscribeviaproxy" should {
    "yield a JSON result that contains a responseCode and a responseText when submitting a POST call" in {
      val jsonRequest = """{
                          "eventSource":"95100000",
                          "sourceNumber":"95100000",
                          "locale":"en_US"
                         }""""
      post("/subscribeviaproxy", jsonRequest.getBytes ,contentType) {
        log.info("responseBody is [{}]", body)
        status should equal (200)
        body should include ("responseCode")
        body should include ("responseMessage")
      }
    }
  }
}

SBT始终失败并出现以下错误:

> test-only com.foo.bar.http.server.mytestservice.MyTestServiceHttpServiceSpec
10:11:46.928  [INFO ] jetty-9.0.2.v20130417
10:11:46.995  [INFO ] started o.e.j.s.ServletContextHandler@1285302{/,file:/P:/git/mediationdal/src/main/webapp/,AVAILABLE}
10:11:47.026  [INFO ] Started ServerConnector@5f57c0{HTTP/1.1}{0.0.0.0:49472}
10:11:47.777  [INFO ] Graceful shutdown org.eclipse.jetty.server.Server@1c05bb5 by
10:11:47.781  [INFO ] Stopped ServerConnector@5f57c0{HTTP/1.1}{0.0.0.0:0}
10:11:47.782  [INFO ] stopped o.e.j.s.ServletContextHandler@1285302{/,file:/P:/git/mediationdal/src/main/webapp/,UNAVAILABLE}
[info] LoyaltyPointsHttpServiceSpec:
[info] An invocation to /subscribeviaproxy
[info] - yield a JSON result that contains a responseCode and a responseText when submitting a POST call *** FAILED ***
[info]   org.apache.http.NoHttpResponseException: The target server failed to respond
[info]   at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
[info]   at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
[info]   at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
[info]   at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
[info]   at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
[info]   at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
[info]   at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
[info]   at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
[info]   at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:717)
[info]   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:522)
[info]   ...
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error]         com.foo.bar.http.server.mytestservice.MyTestServiceHttpServiceSpec
[error] (test:test-only) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 15 s, completed May 8, 2013 10:11:47 AM
>

我可以看到码头服务器正在启动然后被关闭,但我真的不明白为什么。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

管理来解决问题,所有需要做的就是在SBT中设置一个选项来分叉JVM进行测试:

fork in Test := true,

之后的测试就像魅力一样。