Search code examples
scalaplayframeworkplayframework-2.0scalatest

Mock Play JDBC connection in ScalaTest test


I have the following Play for Scala Controller that injects a JDBC connection:

class MyController @Inject() (db1: play.api.db.Database) extends Controller  {
    // some code
}

In my application.conf I have the related configuration:

# JDBC configuration
  db.default.driver=com.mysql.jdbc.Driver
  db.default.url="jdbc:mysql://localhost:3306/db0001"
  db.default.username=root
  db.default.password=xxxxxxx

Now, what I need is to inject in a ScalaTest test a different database (also defined in application.conf). Should be declared in the test as follows:

val controller = new MyController (mockDB)

How can this be achieved?


Solution

  • Declare it like this:

        val mockDB = Databases(
          driver = "com.mysql.jdbc.Driver",
          url = "jdbc:mysql://localhost/db0001"
        )