play framework use in memory h2 database for unit tests play framework use in memory h2 database for unit tests database database

play framework use in memory h2 database for unit tests


From an actual application I'm developing:

import play.api.inject.bindimport org.scalatest.mock.MockitoSugarimport play.api.Applicationimport play.api.inject.guice.GuiceApplicationBuilderimport database.AccountDAOimport play.api.Configurationimport play.api.Modeclass AccountDAOSpec extends Specification with MockitoSugar { // we add mockito to show that you can also bind your mocksval companyAccountDAOMock = mock[CompanyAccountDAO] // let us create a company account DAO mock  def app = new GuiceApplicationBuilder() // you create your app    .configure(      Configuration.from(        Map( // a custom configuration for your tests only          "slick.dbs.default.driver" -> "slick.driver.H2Driver$",          "slick.dbs.default.db.driver" -> "org.h2.Driver",          "slick.dbs.default.db.connectionPool" -> "disabled",          "slick.dbs.default.db.keepAliveConnection" -> "true",          "slick.dbs.default.db.url" -> "jdbc:h2:mem:test",          "slick.dbs.default.db.user" -> "sa",          "slick.dbs.default.db.password" -> "")))    .bindings(bind[AccountDAO].to[AccountDAOImpl]) // here you can define your bindings for an actual implementation (note the use of square brackets)    .bindings(bind[CompanyAccountDAO].to(companyAccountDAOMock)) // or bind to your mock (note the use of parentheses)    .in(Mode.Test)    .build()  "AccountDAO" should {    "throw an Exception when adding a user with an invalid data" in new WithApplication(app) { // here you can use the application you just created, it uses the db you defined for your tests      val app2dao = Application.instanceCache[AccountDAO]      val accountDAO = app2dao(app) // with this you get the DAO injected      accountDAO.addAccount(testAccount).run must throwAn[Exception]     }  }}


You should remove "test".Thus your first line should be:

running(fakeApplication(inMemoryDatabase()), new Runnable() {     //test specific code});