specs2 - 无法创建实例

时间:2014-02-17 13:34:10

标签: scala specs2

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

2 个答案:

答案 0 :(得分:6)

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

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
}

答案 1 :(得分:4)

我使用specs2版本2.3.10 on Scala 2.10得到了一个非常类似的错误。升级到2.3.13会使错误消息提供更多信息,并为根本原因提供额外的堆栈跟踪。这个较新的版本是在最近发布的(本帖发布前8天!),所以希望你能够容纳更新......

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