title: Is it possible to mock DB connection in play functional testing and how?
body: <p>Some of my controllers rely on DB connection and structured as follows:</p>
  
  <pre><code>def getAll(revId: Muid) = Action { implicit request =&gt;
          DB.withConnection { implicit connection =&gt;
  ...
  </code></pre>
  
  <p>I am trying to create a unit test for it with all mocked dependencies, including the connection as well. Now, injection of dependencies is easily done through Guice. However, I am struggling to find a way to mock implicit connection. And, eventually, the test is trying to connect to my default DB in test.</p>
  
  <p>Is it even possible to mock implicits, given this situation, and how?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So, after playing with this thing for a while, I got the following:
  My class under test:</p>
  
  <pre><code>class ChecklistCreationScheduler @Inject()(jobScheduler: JobScheduler,
                                             dBApi: DBApi,
                                             futureChecklistRepository: FutureChecklistRepository) extends ClassLogger{
  def scheduleSingleFutureChecklistJob(futureChecklistId: Muid): Unit = {
      logger.info(s"Preparing to schedule one time future checklist job for future checklist id '${futureChecklistId.uuid}'")
      val db = dBApi.database("default")
      logger.info("Database" + db)
      db.withConnection { implicit connection =&gt;
        logger.info("Connection" + connection)
        ...
      }
  }
  }
  </code></pre>
  
  <p>And the test:</p>
  
  <pre><code>"ChecklistCreationScheduler#scheduleSingleFutureChecklistJob" should {
        "schedule a single job through a scheduler" in {
          val futureChecklistId = Muid.random()
  
          val jobScheduler = mock[JobScheduler]
  
          val connection = mock[Connection]
          val DB = mock[Database]
          DB.getConnection returns connection
  
          val dbApi = mock[DBApi]
          when(dbApi.database("default")).thenReturn(DB)
  
          val futureChecklistRepository = mock[FutureChecklistRepository]
          doReturn(Option.empty).when(futureChecklistRepository).getById(futureChecklistId)(connection)
  
          val chCreationScheduler = new ChecklistCreationScheduler(jobScheduler, dbApi, futureChecklistRepository)
  
          chCreationScheduler.scheduleSingleFutureChecklistJob(futureChecklistId) must throwA[UnexpectedException]
        }
      }
  </code></pre>
  
  <p>When I execute the test, it seems like the execution does not even get into the block of <code>withConnection</code>. (I am never getting to this line: <code>logger.info("Connection" + connection)</code>).</p>
  
  <p>Any idea?</p>