加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 是否有可能在播放功能测试中模拟数据库连接以及如何?

发布时间:2020-12-16 10:00:30 所属栏目:安全 来源:网络整理
导读:我的一些控制器依赖于数据库连接,结构如下: def getAll(revId: Muid) = Action { implicit request = DB.withConnection { implicit connection =... 我正在尝试使用所有模拟的依赖项为它创建单元测试,包括连接.现在,通过Guice可以轻松地注入依赖项.但是,我
我的一些控制器依赖于数据库连接,结构如下:

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

我正在尝试使用所有模拟的依赖项为它创建单元测试,包括连接.现在,通过Guice可以轻松地注入依赖项.但是,我正在努力寻找模拟隐式连接的方法.最后,测试试图连接到测试中的默认DB.

在这种情况下,甚至可以模仿隐含,以及如何?

UPDATE

所以,在玩了这个东西一段时间之后,我得到了以下内容:
我的班级正在测试中:

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)
      ...
    }
}
}

而且测试:

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

当我执行测试时,似乎执行甚至没有进入withConnection块. (我永远不会到达这一行:logger.info(“连接”连接)).

任何的想法?

解决方法

以下是如何使用 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."))
  }
}

你可以写一个Specification like this:

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

当然,可能需要一些其他的模拟来处理你的(整个)用例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读