Skip to content

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

Description

@jpartner

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions