gpt4 book ai didi

scala - Scalatest 的 Wiremock 集成问题

转载 作者:行者123 更新时间:2023-12-05 05:11:11 28 4
gpt4 key购买 nike

我在将 Wiremock 与 Scalatest 规范集成时遇到了问题。我正在使用此版本的 Wiremock:

"com.github.tomakehurst"  %  "wiremock-jre8"       % "2.22.0"    % "test",

我创建了一个 WiremockSpec 如下所示:

trait WiremockSpec extends BeforeAndAfterAll { self: Suite =>

import WireMockConfiguration._

protected val wiremockServer = new WireMockServer(options().dynamicPort())

override protected def beforeAll(): Unit = {
super.beforeAll()
wiremockServer.start()
}

override protected def afterAll(): Unit = {
wiremockServer.stop()
super.afterAll()
}

// some helper methods

}

然后将其混合到我的规范中,如下所示:

class MySpec() extends PlaySpec with Matchers with MockitoSugar with GuiceOneAppPerSuite
with ScalaFutures with WiremockSpec with IntegrationPatience

当我运行测试时,出现此错误:

An exception or error caused a run to abort: Not listening on HTTP port. The WireMock server is most likely stopped 
java.lang.IllegalStateException: Not listening on HTTP port. The WireMock server is most likely stopped
at com.google.common.base.Preconditions.checkState(Preconditions.java:507)
at com.github.tomakehurst.wiremock.WireMockServer.port(WireMockServer.java:178)
at MySpec.<init>(MySpec.scala:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.scalatest.tools.Runner$.genSuiteConfig(Runner.scala:1428)
at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$8(Runner.scala:1236)
at scala.collection.immutable.List.map(List.scala:286)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1235)
at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:1031)
at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:1010)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1506)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
at org.scalatest.tools.Runner$.run(Runner.scala:850)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:131)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

我试过了 this解决方案,但我仍然遇到同样的错误。有人知道为什么会这样吗?我怀疑这可能与特征初始化的方式有关?

最佳答案

最终追踪到 PlaySpec 特性。用 FlatSpec 交换它可以避免与 wiremock 服务器启动发生冲突。与 WordSpec 一样,它也有冲突。 FlatSpec 工作正常。我仍然需要深入研究为什么会发生这种情况。

关于scala - Scalatest 的 Wiremock 集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669762/

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