playframework-2.1

How can I specify a different config file for testing in Play 2.1


I would like to define different database connections for multiple test environments(Production, Staging, Development). After reading the post "How do I specify a config file with sbt 0.12.2 for sbt test?" it seems that it was possible in earlier versions of Play, by using the follwing SBT setting:

val main = play.Project(appName, appVersion, appDependencies).settings(
    javaOptions in Test += "-Dconfig.file=conf/test.conf"
)

But if I use this setting in my Build.scala, I get the following error:

not found: value javaOptions

So my question is, how can I define different connections for different test environments?


Edit: A possible workaround would be to override the default setting during testing. This can be done with a environment variable.

object Config {
  var defaultConfig = Map(
    "db.default.user" -> "user",
    "db.default.pass" -> "************"
  )

  def additionalConfiguration(): Map[String, _] = sys.env.getOrElse("PLAY_TEST_SCOPE", "") match {
    case "development" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/development"
      defaultConfig
    }
    case "staging" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/staging"
      defaultConfig
    }
    case "production" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/production"
      defaultConfig
    }
    case _ => {
      throw new Exception("Environment variable `PLAY_TEST_SCOPE` isn't defined")
    }
  }
}

And then running a fake application with this configuration.

FakeApplication(additionalConfiguration = Config.additionalConfiguration())

Solution

  • javaOptions is contained within the Keys object.

    Make sure that you use the proper import in your Build.scala file:

    import Keys._