Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specs2 -- Could not create an instance

Tags:

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

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

like image 630
jilen Avatar asked Feb 17 '14 13:02

jilen


2 Answers

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
}
like image 58
Eric Avatar answered Oct 10 '22 20:10

Eric


I was getting a very similar error using specs2 version 2.3.10 on Scala 2.10. Upgrading to 2.3.13 makes the error messages much more informative and provides an extra stacktrace to the root cause. This newer version was released very recently (8 days before this post!), so hopefully you're able to accommodate an update...

Some of my issues ended up being related the val vs. lazy val problem like in the accepted answer; however, I'm now able to pinpoint the exact line that these errors are occurring on in addition to debugging other initialization problems as well.

like image 35
rkoval Avatar answered Oct 10 '22 19:10

rkoval