首页 > 解决方案 > Wiremock Integration Issue with Scalatest

问题描述

I am having trouble integrating Wiremock with a Scalatest specification. I am using this version of Wiremock:

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

I created a WiremockSpec looks like this:

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

}

And then mix it into my specification as follows:

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

When I run the test, I get this error:

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)

I tried this solution but I still get the same error. Anybody know why this is happening? I am suspecting it maybe down to the way the trait is being initialised?

标签: scalascalatestwiremock

解决方案


最后追查到这个PlaySpec特质。将其替换为FlatSpec避免与wiremock 服务器启动发生冲突。与 相同WordSpec,它也会发生冲突。FlatSpec工作正常。我仍然需要深入研究为什么会发生这种情况。


推荐阅读