我想使用与生产中使用的相同数据库设置为 Play 2 Scala 应用程序运行单元测试:Slick with Postgres。以下失败并显示“java.sql.SQLException:尝试从已关闭的池中获取连接”。在第二次测试。
package controllers
import org.specs2.mutable._
import play.api.db.DB
import play.api.Play.current
import play.api.test._
import play.api.test.Helpers._
import scala.slick.driver.PostgresDriver.simple._
class BogusTest extends Specification {
def postgresDatabase(name: String = "default",
options: Map[String, String] = Map.empty): Map[String, String] =
Map(
"db.test.driver" -> "org.postgresql.Driver",
"db.test.user" -> "postgres",
"db.test.password" -> "blah",
"db.test.url" -> "jdbc:postgresql://localhost/blah"
)
def fakeApp[T](block: => T): T =
running(FakeApplication(additionalConfiguration =
postgresDatabase("test") ++ Map("evolutionplugin" -> "disabled"))) {
def database = Database.forDataSource(DB.getDataSource("test"))
database.withSession { implicit s: Session => block }
}
"Fire 1" should {
"do something" in fakeApp {
success
}
}
"Fire 2" should {
"do something else" in fakeApp {
success
}
}
}
我像这样运行测试:
$ play -Dconfig.file=`pwd`/conf/dev.conf "test-only controllers.BogusTest"
另外两个谜团:
1)所有测试都运行,即使我只要求运行 BogusTest
2)始终使用application.conf,而不是def.conf,驱动信息来自application.conf,而不是代码中配置的信息。