scala - specs2 -- 无法创建实例

标签 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

最佳答案

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
}

您可以将这些值变成惰性值以避免这种情况:

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
}

关于scala - specs2 -- 无法创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830495/

相关文章:

scala - Akka Actors 可以替代服务层吗?

scala - Scala 测试中应该和必须有什么区别?

elasticsearch - 如何运行嵌入式 Elasticsearch 实例进行测试

scala - 在运行时获取 Scala 变量名

java - 添加静态方法以促进更清洁的单元测试 - 好的做法?

list - 从不可变列表中获取 "remove"一个元素的惯用 Scala 方法是什么?

scala - 模拟specs2中的slick.dbio.DBIO组成

scala - 如何使用 Spray 创建带有表单字段内容的 POST 请求?

playframework - 如何将 Play 2.4.x 与 scalaz 7.2.0 一起使用

scala - 创建数据帧时如何解决 scala.MatchError