【问题标题】:specs2 -- Could not create an instancespecs2 -- 无法创建实例
【发布时间】:2014-07-16 01:43:30
【问题描述】:
testOnly play.api.weibo.StatusesShowBatchSpec
[error] Could not create an instance of play.api.weibo.StatusesShowBatchSpec
[error]   caused by java.lang.Exception: Could not instantiate class play.api.weibo.StatusesShowBatchSpec: null
[error]   org.specs2.reflect.Classes$class.tryToCreateObjectEither(Classes.scala:93)
[error]   org.specs2.reflect.Classes$.tryToCreateObjectEither(Classes.scala:211)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
...

规格

package play.api.weibo

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

class StatusesShowBatchSpec extends ApiSpec {

  "'statuses show batch' api" should {
    "read statuses" in {
      val api = StatusesShowBatch(
        accessToken = testAdvancedToken,
        ids = "3677163356078857")
      val res = awaitApi(api)
      res.statuses must have size (1)
    }

} }

在此处查看完整代码https://github.com/jilen/play-weibo/tree/spec2_error

完整的堆栈跟踪 https://gist.github.com/jilen/9050548

【问题讨论】:

  • 我收到匹配错误:`scala.MatchError: Left(play.api.weibo.WeiboApiError: Weibo api error: source paramter(appkey) is missing, code: 10006: request: / 2/statuses/show_batch.json)(属于 scala.util.Left 类)`。你确定你还没有修复它吗?
  • 谢谢,我已经切换到scalatest,然后发现问题,似乎是spec2的异常吃

标签: scala specs2


【解决方案1】:

ApiSpec 类中,您有一些变量在实例化时可能为空:

val cfg = ConfigFactory.load("http.conf")
val testToken = cfg.getString("token.normal")
val testAdvancedToken = cfg.getString("token.advanced")

implicit val http = new SprayHttp {
  val config = new SprayHttpConfig {
    val system = ActorSystem("test")
    val gzipEnable = true
  }
  val context = config.system.dispatcher
}

你可以把这些 val 变成惰性 val 来避免这种情况:

lazy val cfg = ConfigFactory.load("http.conf")
lazy val testToken = cfg.getString("token.normal")
lazy val testAdvancedToken = cfg.getString("token.advanced")

implicit lazy val http = new SprayHttp {
  lazy val config = new SprayHttpConfig {
    val system = ActorSystem("test")
    val gzipEnable = true
  }
  val context = config.system.dispatcher
}

【讨论】:

  • specs2 似乎丢失了一些堆栈跟踪
  • 默认情况下确实有一些过滤。您可以使用test-only *StatusesShowBatchSpec* -- fullstacktrace 获取完整的堆栈跟踪。
  • github.com/etorreborre/specs2/pull/236 堆栈跟踪不完整然后我提交这个。
【解决方案2】:

我在使用 specs2 版本 2.3.10 on Scala 2.10 时遇到了一个非常相似的错误。升级到2.3.13 可以使错误消息提供更多信息,并为根本原因提供额外的堆栈跟踪。这个较新的版本是最近发布的(比这篇文章早 8 天!),所以希望你能够适应更新......

我的一些问题最终与vallazy val 问题有关,就像在接受的答案中一样;但是,除了调试其他初始化问题之外,我现在还能够查明发生这些错误的确切位置。

【讨论】:

  • 啊哈。看起来它当时已迁移到实际版本中。感谢您的修复!
猜你喜欢
  • 2020-01-16
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
相关资源
最近更新 更多