gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:55:44 25 4
gpt4 key购买 nike

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

我使用的版本:斯卡拉特拉:2.2.1斯卡拉测试:1.9.1ScalatraScalatest:2.2.1 jetty :9.0.2.v20130417斯卡拉:2.10.1SBT: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
>

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

提前谢谢你。

最佳答案

设法解决了这个问题,所有需要做的就是在 SBT 中设置一个选项来 fork JVM 以进行测试:

fork in Test := true,

之后测试运行得非常顺利。

关于Scalatrasuite 测试总是在 sbt 中失败,在 IDE 中运行测试时成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16713822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com