1

I am writing Specs2 tests for methods returning futures in a project using Scala and Play framework. Documentation and answers to this question recommend using the await modifier, which requires to add implicit ExecutionEnv. A minimal working example (adapted from one of the mentioned answers):

class FutureSpec extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in { implicit ee: ExecutionEnv =>
      Future(1) must be_>(0).await
    }
  }
}

But some of my tests require WithApplicationLoader. If I add it to the example, it does not compile:

class FutureSpec extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in new WithApplicationLoader { implicit ee: ExecutionEnv =>
      Future(1) must be_>(0).await
    }
  }
}

WithApplication instead of WithApplicationLoader has exactly the same effect (expectedly).

Is it possible to use WithApplicationLoader together with implicit ExecutionEnv?

Unfortunately, the second option described in the documentation -- moving ExecutionEnv to the class constructor instead of a particular method -- is not available. This specification:

class FutureSpec(implicit ee: ExecutionEnv) extends mutable.Specification {
  "Even in future one" should {
    "be greater than zero" in new WithApplicationLoader {
      Future(1) must be_>(0).await
    }
  }
}

works, but it is ignored by IntelliJ Idea (I can run such a specification separately, but the configuration running all tests in the project does not execute it).

Community
  • 1
  • 1
Ruslan Batdalov
  • 793
  • 1
  • 8
  • 21

2 Answers2

1

This works for me with IntelliJ 2016.1.3:

import play.api.test.{PlaySpecification, WithApplication}
import org.specs2.concurrent.ExecutionEnv
import scala.concurrent.Future

class FutureSpec extends PlaySpecification {
  implicit val ee = ExecutionEnv.fromGlobalExecutionContext
//  // or this:
//  implicit val ee = ExecutionEnv.fromExecutionContext(play.api.libs.concurrent.Execution.Implicits.defaultContext)

  "Even in future one" should {
    "be greater than zero" in new WithApplication {
      Future(1) must be_>(0).await
    }
  }
}

Here is my build.sbt:

name := "throwaway"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies += "org.specs2" %% "specs2-core" % "3.8.5.1" % "test"

libraryDependencies += "com.typesafe.play" %% "play" % "2.5.9" % "test"

libraryDependencies += "com.typesafe.play" %% "play-specs2" % "2.5.9" % "test"

libraryDependencies += "com.typesafe.play" %% "play-test" % "2.5.9" % "test"
Jamie
  • 5,994
  • 1
  • 18
  • 15
0

You can always do this

class TestSpec extends mutable.Specification {
  "test1" >> { implicit ee: ExecutionEnv =>
    new WithApplicationLoader {
      ok
    }
  }
}
Eric
  • 15,494
  • 38
  • 61
  • Unfortunately, that does not work either. Your example as it is compiles, but as soon as I replace 'ok' with 'Future(1) must be_>(0).await', compilation fails with the following message: "Error:scalac: symbol value ee$1#94964 does not exist in testSpec$$anonfun$1$$anon$1.delayedEndpoint$TestSpec$$anonfun$1$$anon$1$1, which contains locals." – Ruslan Batdalov Nov 01 '16 at 11:35
  • Ouch, this is a compiler bug then. From the error message it looks like you have macros somewhere which are misbehaving. I would need a more complete view of the code to get an idea on how to fix this. I actually had a similar issue last week which was fixed by annotating a `lazy val`. – Eric Nov 01 '16 at 16:03
  • It may be influenced by Play framework support of Specs2. If you are interested in investigating this issue, I committed a minimal project to https://github.com/linnando/specs2test. It is just a blank Activator Play Scala Seed template with only two changes: using Specs2 instead of ScalaTest and adding our TestSpec instead of the shipped specifications. In fact, I have already solved my original problem by following Jamie's advice, but if you need any additional information, I am ready to help. – Ruslan Batdalov Nov 01 '16 at 20:48
  • This is a "DelayedInit" bug, thanks for the minimal project. – Eric Nov 02 '16 at 13:49