Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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())
like image 231
akkie Avatar asked Mar 26 '13 12:03

akkie


People also ask

What is application conf file?

An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.

How do I change port Play framework?

Go to the project directory and just say play (and nothing after that). That will open the play console. Next, say run 8080. That will start play on port 8080.

What is ConfigFactory in Scala?

Configurations from a file By default, the ConfigFactory looks for a configuration file called application. conf. If willing to use a different configuration file (e.g.: another. conf), we just need to indicate a different file name and path to load (e.g.: ConfigFactory.


1 Answers

javaOptions is contained within the Keys object.

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

import Keys._
like image 199
ndeverge Avatar answered Oct 07 '22 16:10

ndeverge