I'm working on this github project that uses Play 2.5.10 and Slick 3.1.1 (the issue can be reproduced there by running sbt test
but can also be checked directly in travis CI). I use a Postgres database configuration default
for development and production. I then use a H2 in memory database called test
for testing. The default
database is configured in conf/application.conf
whereas the test
database is configured in conf/application.test.conf
.
The problem is that for testing I initialize the database with name test
but the Application built with GuiceApplicationBuilder
is still picking up the default
one.
This line is in my build.sbt
to pick up the test configuration:
javaOptions in Test += "-Dconfig.file=conf/application.test.conf"
and this is the content of that file:
include "application.conf"
slick.dbs {
test {
driver="slick.driver.H2Driver$"
db.driver="org.h2.Driver"
db.url="jdbc:h2:mem:test;MODE=PostgreSQL"
db.username="sa"
db.password=""
}
}
My DaoFunSpec
base class looks like this:
package dao
import org.scalatest.{BeforeAndAfterAll, FunSpec}
import org.scalatestplus.play.OneAppPerSuite
import play.api.Application
import play.api.db.evolutions.Evolutions
import play.api.db.DBApi
abstract class DaoFunSpec extends FunSpec with OneAppPerSuite with BeforeAndAfterAll {
lazy implicit val db = app.injector.instanceOf[DBApi].database("test")
override def beforeAll() {
Evolutions.applyEvolutions(db)
}
override def afterAll() {
Evolutions.cleanupEvolutions(db)
}
def userDao(implicit app: Application) = {
Application.instanceCache[UserDao].apply(app)
}
}
Note the line app.injector.instanceOf[DBApi].database("test")
but still Play tries to connect to the default
database.