So the below with GuiceOneServerPerSuite is fine but switching that to GuiceOneServerPerTest means the application is null in beforeAll():
import org.scalatest.BeforeAndAfterEach
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.Configuration
import play.api.test.Injecting
class BeforeAndAfter extends PlaySpec with Injecting with GuiceOneServerPerSuite with
BeforeAndAfterEach {
"How do I get dependencies in before and after" should {
"2 + 2 = 4" in{
assert((2+2) == 4)
val playConfig = inject[Configuration]
println(playConfig)
}
}
override protected def beforeEach(): Unit = {
val playConfig = inject[Configuration]
println(playConfig)
}
override protected def afterEach(): Unit = {
val playConfig = inject[Configuration]
println(playConfig)
}
}
But the below fails with a null pointer in beforeEach:
**Exception encountered when invoking run on a nested suite.
java.lang.NullPointerException
at play.api.test.Injecting.inject(Helpers.scala:631)
at play.api.test.Injecting.inject$(Helpers.scala:630)
at BeforeAndAfter.inject(BeforeAndAfter.scala:7)
at BeforeAndAfter.beforeEach(BeforeAndAfter.scala:22)**
import org.scalatest.BeforeAndAfterEach
import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.Configuration
import play.api.test.Injecting
class BeforeAndAfter extends PlaySpec with Injecting with GuiceOneServerPerTest with
BeforeAndAfterEach {
"How do I get dependencies in before and after" should {
"2 + 2 = 4" in{
assert((2+2) == 4)
val playConfig = inject[Configuration]
println(playConfig)
}
}
//runs fine if below commented out
override protected def beforeEach(): Unit = {
val playConfig = inject[Configuration]
println(playConfig)
}
override protected def afterEach(): Unit = {
val playConfig = inject[Configuration]
println(playConfig)
}
}
So the below with
GuiceOneServerPerSuiteis fine but switching that toGuiceOneServerPerTestmeans the application is null inbeforeAll():But the below fails with a null pointer in
beforeEach: