使用mockito库进行Scala单元测试



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

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是一个抽象类,它从postgres数据库返回所有模块的列表,程序显示如上所述的错误,实际上语句return (x)没有编译,并且在编译时显示此错误:

Error:(32, 47) overloaded method value thenReturn with alternatives:
  (x$1: scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]],x$2: scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]]*)org.mockito.stubbing.OngoingStubbing[scala.concurrent.Future[Seq[com.reactore.launchpad.entity.Modules]]] <and>
  (x$1: 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)

我不知道怎么了

(评论):

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

相关内容

  • 没有找到相关文章

最新更新