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

前端 未结 4 1427
误落风尘
误落风尘 2021-02-02 17:06

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 w

相关标签:
4条回答
  • 2021-02-02 17:54

    Update for Play 2.5.x

    Explicit import for import Keys._ is no longer required, and the vm param for config resource location has changed.

    javaOptions in Test += "-Dconfig.resource=<conf_name>.conf"
    
    0 讨论(0)
  • 2021-02-02 18:02

    You can run your application from console with alternative config file, anyway you need to use -Dconfig.file with full path as there are some problems ... that I can't realize with other option. For an example in unix env:

    play -Dconfig.file=/home/akkie/play/some-project/conf/local_akkie_dev.conf "~run 9123"
    

    Of course for easier launching you can create bash script for calling this line.

    Edit: Note that you don't need to write whole config in each additional config file, as you can just include your main config at beginning and then overwrite only required properties :

    include "application.conf"
    
    key.to.override=blah
    

    Take a look to the official doc

    0 讨论(0)
  • 2021-02-02 18:04

    we can mix the above solutions, to pass the config file as a parameter to sbt.
    This will be useful to integrate the test in CI pipeline

    First, in the Build.scala file

    val testOptions = "-Dconfig.file=conf/" + Option(System.getProperty("test.config")).getOrElse("application") + ".conf"
    
    val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
        javaOptions in Test += testOptions
    )
    

    Then, in the command line to run the test with integ.conf

    sbt -Dtest.config=integ test
    

    to use the default application.conf

    sbt test 
    
    0 讨论(0)
  • 2021-02-02 18:08

    javaOptions is contained within the Keys object.

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

    import Keys._
    
    0 讨论(0)
提交回复
热议问题