使用mockito库进行Scala单元测试

时间:2015-09-12 10:48:22

标签: scala testing mockito akka-http

我在akk-http中编写了休息测试用例,我在语句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数据库中所有模块的列表,程序显示如上所述的错误,实际上语句theReturn(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)

我不知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

(来自评论):

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

相关问题