Scalatest 的 Wiremock 集成问题

Wiremock Integration Issue with Scalatest

我在将 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(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(Runner.scala:1031)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$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 工作正常。我仍然需要深入研究为什么会发生这种情况。