scala - 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?
解决方案
最后追查到这个PlaySpec
特质。将其替换为FlatSpec
避免与wiremock 服务器启动发生冲突。与 相同WordSpec
,它也会发生冲突。FlatSpec
工作正常。我仍然需要深入研究为什么会发生这种情况。
推荐阅读
- android - 从用户地图上的 4 个角检索纬度和经度
- c# - 无法从 appsettings.json 的 IConfiguration 提供程序映射实例
- reactjs - 选择选项以刷新日期选择器
- javascript - 如果 de-DE 语言环境包含其他选项,则使用 .toLocaleDateString() 不能正确缩短 "{month: 'short'}" 的月份
- reactjs - Django Rest 框架 SQL 查询 CORS
- r - 带约束的非参数回归
- python - 对数据框行的操作
- sql - SQL - 使用服务组合查找帐户
- sqlite - 限制具有别名的列的结果
- sql - 为什么向 LIKE 添加第二个选项似乎撤消了我的 date = 功能?