jeudi 3 mars 2016

Play 2.3 FakeApplication mode not setting in test?

I'm using play 2.3 and have some configuration in my GlobalSettings that change based on the mode of the application. So I have something like this:

object Global extends GlobalSettings {
    override def onLoadConfig(config: Configuration, path: java.io.File, classloader: ClassLoader, mode: Mode.Mode) = {
        println(mode)
        val customConfig = //Based on mode.*
        config ++ configuration ++ Configuration(ConfigFactory.parseMap(customConfig))
    }
}

And then am trying to write tests to ensure that this behavior works:

class MyTest extends PlaySpec {

    val testApp = FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    )

    val devApp = new FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    ) {
        override val mode = Mode.Dev
    }

    val prodApp = new FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    ) {
        override val mode = Mode.Prod
    }

    "ThisNonWorkingTestOfMine" must {

        "when running application in test mode have config.thing = false" in running(testApp) {
            assertResult(Mode.Test)(testApp.mode)
            assertResult(false)(testApp.configuration.getBoolean("config.thing").get)
        }

        "when running application in dev mode have config.thing = false" in running(devApp) {
            assertResult(Mode.Dev)(devApp.mode)
            assertResult(false)(devApp.configuration.getBoolean("config.thing").get)
        }

        "when running application in prod mode have config.thing = true" in running(prodApp) {
            assertResult(Mode.Prod)(prodApp.mode)
            assertResult(true)(prodApp.configuration.getBoolean("config.thing").get)
        }
    }

}

And when I run these tests I see something a bit odd from my handy println:

Test
null
null
[info] MyTest:
[info] ThisNonWorkingTestOfMine
[info] play - Starting application default Akka system.
[info] play - Shutdown application default Akka system.
[info] - must when running application in test mode have config.thing = false
[info] play - Application started (Dev)
[info] - must when running application in dev mode have config.thing = false
[info] play - Application started (Prod)
[info] - must when running application in prod mode have config.thing = true *** FAILED ***
[info]   Expected true, but got false (MyTest.scala:64)
[info] ScalaTest

How do I properly set the mode of the FakeApplication in Play 2.3? The way I have it now is based on a page from Mastering Play but clearly that isn't the way to go when using onLoadConfig it seems

Aucun commentaire:

Enregistrer un commentaire