title: How to set an in memory test database for my Scala Play2 CRUD application?
body: <p>I'm continuing my exploration of the Play framework and its related components. I used the template for CRUD application with a connection to a PostgreSQL database to begin with. It splits the application in models, repositories, controllers and views. This works well.
  Now, I'm trying to create some tests for this application with Specs2. More precisely, I'm trying to test the repository. It is defined as follow:</p>
  
  <pre><code>package dal
  import javax.inject.{ Inject, Singleton }
  import play.api.db.slick.DatabaseConfigProvider
  import slick.driver.JdbcProfile
  
  import models.Cat
  import scala.concurrent.{ Future, ExecutionContext }
  
  @Singleton
  class CatRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  ...
  }
  </code></pre>
  
  <p>I would like to set an in memory database which would be created (schema, evolutions) before all tests, destroyed after all tests, populated (data, probably with direct SQL) and flushed around each test. I would like to pass it on to my repository instance which I would then use to perform my test. Like:</p>
  
  <pre><code>val repo = new CatRepository(inMem_DB)
  </code></pre>
  
  <p>So how do I go about:
  1) creating this db and applying the evolutions?
  maybe:</p>
  
  <pre><code>trait TestDB extends BeforeAfterAll {
      var database: Option[Database] = None
  
      def before = {
          database = Some(Databases.inMemory(name = "test_db"))
          database match {
              case Some(con) =&gt; Evolutions.applyEvolutions(con)
              case _ =&gt; None
          }
          println("DB READY")
      }
  
      def after = {
          database match {
              case Some(con) =&gt; con.shutdown()
              case _ =&gt; None
          }
      }
  }
  </code></pre>
  
  <p>Using a var and always making a "match/case" when I need to use the db isn't convenient. I guess there is a much better to do this...</p>
  
  <p>2) Populate and flush around each test?
  Shall I create a trait extending Around, just as with BeforeAfterAll?</p>
  
  <p>3) Create on of these play.api.db.slick.DatabaseConfigProvider from the database?
  Any link showing how to do this?</p>
  
  <p>I found few examples which where covering this with running a FakeApplication, but I assume there is a way to pass somehow the db to such repository object outside of a running application..?</p>
  
  <p>Thank you for helping.
  Cheers!</p>