我在 sbt 中使用 ScalaTest 进行 ScalatraSuite 单元测试时遇到了一些问题。
我正在使用的版本:Scalatra:2.2.1 ScalaTest:1.9.1 ScalatraScalatest:2.2.1 Jetty:9.0.2.v20130417 Scala: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
>
我可以看到码头服务器正在启动然后被关闭,但我真的不明白为什么。
先感谢您。