Search code examples
scalaspecs2

specs2 -- Could not create an instance


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)
...

The spec

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)
    }

} }

See full code here https://github.com/jilen/play-weibo/tree/spec2_error

Full stacktrace https://gist.github.com/jilen/9050548


Solution

  • In the ApiSpec class you have a few variables which might be null at instantiation time:

    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
    }
    

    You can turn those vals into lazy vals to avoid this situation:

    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
    }