Is it possible to tame a DB connection in functional testing of a game and how?

Some of my controllers rely on a database connection and are structured as follows:

def getAll(revId: Muid) = Action { implicit request =>
        DB.withConnection { implicit connection =>
...

I am trying to create a unit test for it with all the mocked dependencies, including the connection. Dependency injection is now easy to do through Guice. However, I am struggling to find a way to mock the implicit join. And finally, the test tries to connect to my default database in the test.

Is it even possible to ridicule the implication, given this situation, and how?

UPDATE

So, after playing with this thing for a while I got the following: Test of my class:

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 =>
      logger.info("Connection" + connection)
      ...
    }
}
}

And the test:

"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]
      }
    }

When I execute the test, it seems that the execution does not even fall into the block withConnection. (I never get to this line :) logger.info("Connection" + connection).

Any idea?

+4
1

Dependency Injection, :

, :

package controllers

import javax.inject.Inject

import play.api._
import play.api.db.Database
import play.api.mvc._

class Application @Inject() (database: Database) extends Controller {

  def index = Action { implicit request =>
    database.withConnection { implicit  connection =>
      ???
    }
    Ok(views.html.index("Your new application is ready."))
  }
}

:

import java.sql.Connection

import controllers.Application
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import org.specs2.mock._
import play.api.db.Database
import play.api.mvc.RequestHeader

@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification with Mockito {

  "Application" should {

    "index page" in {
      val connection = mock[Connection]
      val database = mock[Database]
      database.getConnection returns connection

      val controller = new Application(database)

      // You will also need to mock the request
      // so that you can add the expected behavior 
      val request = mock[RequestHeader]
      val result = controller.index(request)

      // do some assert about your result
      result must not beNull
    }
  }
}

, () .

+3

Source: https://habr.com/ru/post/1624855/


All Articles