Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success of before and after injection varies according to GuiceOneServerPerTest or GuiceOneServerPerSuite #121

Open
jpartner opened this issue Sep 14, 2018 · 1 comment

Comments

@jpartner
Copy link

jpartner commented Sep 14, 2018

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)
  }
}
@nicmarti
Copy link

Could it be because beforeEach() is called before newAppForTest(td:TestData):Application from GuiceOneServerPerTest?

So the app is null and you can't use the app in beforeEach().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants