在scala中模拟DB.withConnection

时间:2016-04-11 19:23:41

标签: scala unit-testing anorm

我的应用程序中有一个服务类,看起来像这样:

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

我正在尝试创建一个单元测试,其中连接被模拟。我所做的是注入dbApi并嘲笑:

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

一切都很好编译,这一行的数据库是我的模拟对象: val db = dBApi.database("default")

但是,它不会再进一步​​了。这种方法唯一没有返回的东西。 我究竟做错了什么?是否有可能做我想做的事情?

谢谢,

0 个答案:

没有答案