play.application.loader for Test mode

308 views
Skip to first unread message

Zoro

unread,
Dec 14, 2015, 7:07:37 AM12/14/15
to play-framework
Hello, i have a custom loader like this:

package global

import com.typesafe.config.ConfigFactory
import play.api.Mode._
import play.api.inject.guice.{GuiceApplicationBuilder, GuiceApplicationLoader}
import play.api.{ApplicationLoader, Configuration, Logger}

class CustomApplicationLoader extends GuiceApplicationLoader {
  override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
    val builder = initialBuilder.in(context.environment).overrides(overrides(context): _*)

    context.environment.mode match {
      case Prod =>
        // start mode
        Logger.info("Starting Prod mode...")
        builder.loadConfig(context.initialConfiguration)
      case Dev =>
        // run mode
        Logger.info("Starting Dev mode...")
        val devConf = Configuration(ConfigFactory.load("application.dev.conf"))
        builder.loadConfig(devConf)
      case Test =>
        // test mode
        Logger.info("Starting Test mode...")
        val testConf = Configuration(ConfigFactory.load("application.test.conf"))
        builder.loadConfig(testConf)
    }
  }
}

I also added this line to application.conf:
play.application.loader="global.CustomApplicationLoader"

I need this for various configuration files, depending on the startup mode. And this is working for prod or dev mode. But not tests. I wrote simple test: 

import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable._
import play.api.Mode
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.Helpers.running

class Test extends Specification {
  "The 'Hello world' string" should {
    "contain 11 characters" >> { implicit ee: ExecutionEnv =>
      val app = new GuiceApplicationBuilder()
    //    .configure("play.application.loader" -> "global.CustomApplicationLoader")  // this line doesn't help me
        .in(Mode.Test)
        .build()
      running(app){
        "Hello world" must have size 11
      }
    }
  }
}

But Test does not load application.test.conf file. As if Test not using my CustomApplicationLoader


What wrong?

Zoro

unread,
Dec 14, 2015, 7:08:52 AM12/14/15
to play-framework
Forgot to specify -> Play 2.4.4 
Reply all
Reply to author
Forward
0 new messages