使用 mockito 库的 Scala 单元测试

Scala Unit Testing using mockito library

我在 akk-http 中编写其余测试用例,我在语句 when(mockedRepository.getAllFromModule).thenReturn(x) 中收到此错误。我的 Class 如下:

class GetModulesRestTest extends WordSpec with Matchers with ScalatestRouteTest with MockitoSugar {


  val mockedRepository = mock[ImplModuleRepository]
  val dummyRoutes = new GetModulesRest().routes
  val inputData = Modules(1L,Some("ModuleName"), Some("SomeDescription"), false)


val dataJson = """[{"id": 1,"name": "HR","description": "This is about HR module","isRemoved": false}]"""

  "Check Software Test" should {

"check for java and zookeeper installation" in {

  val x = Future(Seq(inputData))

  when(mockedRepository.getAllFromModule).thenReturn(x)
  Get("/getmodules") ~> dummyRoutes ~> check {

    responseAs[String] shouldBe dataJson
  }
}
  }
}

这里 ImplModuleRepository 是一个抽象 class ,它从 postgres 数据库返回所有模块的列表,程序显示如上所述的错误,实际上语句 theReturn(x) 没有编译并且是编译时显示此错误:

Error:(32, 47) overloaded method value thenReturn with alternatives:

  (x: scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]],x: scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]]*)org.mockito.stubbing.OngoingStubbing[scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]]] <and>
  (x: scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]])org.mockito.stubbing.OngoingStubbing[scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]]]
 cannot be applied to (scalaz.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]])
      when(mockedRepository.getAllFromModule).thenReturn(x)

不知道怎么回事

(来自评论):

看起来您正在导入并因此在测试中使用 Future 的 Scalaz 版本,但在 ImplModuleRepository 中没有。