Blankman
Blankman

Reputation: 266900

Controller Spec is using FakeApplication, but can't load test configuration

My HomeControllerSpec looks like:

@RunWith(ClassOf[JUnitRunner])
class HomeControllerSpec extends Specification {

  "HomeController" should {

      object FakeGuiceGlobal extends play.api.GlobalSettings {
          private lazy val injector = {
            Guice.createInjector(new GuiceServicesModule)
          }

          override def getControllerInstance[A](clazz: Class[A]) = {
            injector.getInstance(clazz)
          }

          override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader, mode: Mode.Mode): Configuration = {
            val modeSpecificConfig = config ++ Configuration(ConfigFactory.load("application.test.conf"))
            super.onLoadConfig(modeSpecificConfig, path, classloader, mode)
          }
        }


        "index" in {
          running(FakeApplication(withGlobal = Some(FakeGuiceGlobal))) {

            // test here

          }

        }

   }

}

For some reason it doesn't load the application.test.conf file, it is still reading from the default application.conf file.

Why isn't it reading from the test config file?

If it cant' read the file, why am I not seeing an error if the file path is not correct?

Upvotes: 1

Views: 467

Answers (2)

Reid Spencer
Reid Spencer

Reputation: 2785

The documentation for Play's GlobalSettings.onLoadConfig method states:

Called just after configuration has been loaded, to give the application an opportunity to modify it.

Providing an override of that method does not give you the opportunity to specify a different configuration to load, only to modify it. When you use the ++ operator, it is defined in play like this:

def ++(other: Configuration): Configuration = {
    Configuration(other.underlying.withFallback(underlying))
}

Note how it invokes other.underlying.withFallback which is a call in the embedded Typesafe Config object. Essentially, the configuration from application.test.conf is loaded and used as the fallback for configuration values. Any value you had in the original file is given priority during lookup. Values that only occur in application.test.conf will be found.

FYI: The "credible source" here is the Play code itself.

Upvotes: 1

myborobudur
myborobudur

Reputation: 4435

You need to override the default location when you execute your tests:

-Dconfig.resource=./application.test.conf

Make sure the file path is correct.

Upvotes: 0

Related Questions