【问题标题】:Running tests using ScalaFutures in ScalaTest throws IllegalStateException from time to time在 ScalaTest 中使用 ScalaFutures 运行测试不时抛出 IllegalStateException
【发布时间】:2016-05-28 10:34:29
【问题描述】:

使用 scalatest 运行测试时,我不时收到 IllegalStateException:

future 返回了一个异常类型: java.lang.IllegalStateException,带有消息:无法初始化 执行上下文; AsyncExecutor 已经关闭。

我将 ScalaTest 2.2.6 与 Play framework 2.4 和 Slick 3.1 结合使用。测试如下:

class FooDaoImplSpec
  extends PlaySpec
  with TestConfiguration
  with OneAppPerSuite
  with ScalaFutures
  with IntegrationPatience {

  override lazy val app = new GuiceApplicationBuilder()
    .loadConfig(testConfig)
    .in(Mode.Test)
    .bindings(bind[FooDao].to[FooDaoImpl])
    .build

  "FooDao#findAll" must {
    "return a result set with the length of 3" in {
      val fooDao = app.injector.instanceOf[FooDao]
      val result: Future[Seq[FooModel]] = fooDao.findAll()
      whenReady(result) { r =>
        r must have length (3)
      }
    }
  }
}

fooDao 在上面的例子中使用 Slick 来查询数据库。也许还值得一提的是,FooDaoImpl 使用了 Play 的内部执行上下文 (play.api.libs.concurrent.Execution.Implicits.defaultContext)。

我真的有点卡在这里。正如我已经提到的,异常是在我的一个测试套件中随机抛出的,但只是偶尔抛出。

以前有没有人遇到过同样的问题?任何帮助表示赞赏。

【问题讨论】:

    标签: playframework playframework-2.0 slick scalatest slick-3.0


    【解决方案1】:

    好的,看来我想通了。我不小心让我的测试在parallelExecution 设置为true 运行。以下 sbt 设置为我解决了这个问题:

    parallelExecution in Test := false
    fork in Test := true
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2014-07-26
      • 2013-03-23
      • 1970-01-01
      • 2015-10-25
      • 2016-08-29
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多