我想在不改变数据库的情况下运行模型测试。 我正在使用没有进化的mysql数据库。
是否可以为我的测试指定不同的数据库?或者将mysql架构复制到内存数据库中?
我尝试制作一个单独的配置并使用它:
play test -Dconfig.resource=application.test.conf
没有利润。
答案 0 :(得分:1)
您可以在播放应用程序中使用多个数据库。在这里我给出了三个应用程序数据库的例子。(prod,dev,test)
编写用于生产的conf文件(在conf目录中)
<强> prod.conf 强>
include "application.conf"
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/productiondatabase"
db.default.user="postgres"
db.default.password="postgres"
编写用于开发的conf文件
<强> dev.conf 强>
include "application.conf"
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/developmentdatabase"
db.default.user="postgres"
db.default.password="postgres"
为测试编写conf文件
<强> test.conf 强>
include "application.conf"
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:test"
db.default.user=sa
db.default.password=""
现在写 Global.scala (在app目录中)
import play.api._
import play.api.Logger
import com.typesafe.config.ConfigFactory
import java.io.File
object Global extends GlobalSettings {
override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader,mode: Mode.Mode): Configuration = {
Logger.info("Application configuration file is loading with " + mode.toString + " mode")
val modeSpecificConfig = config ++ Configuration(ConfigFactory.load(s"${mode.toString.toLowerCase}.conf"))
super.onLoadConfig(modeSpecificConfig, path, classloader, mode)
}
}
$ play test =&gt;它会自动连接到测试数据库。
$ play run =&gt;它会自动连接到dev数据库。
$ play start =&gt;它会自动连接到prod数据库。