【问题标题】:Scala Unit Testing using mockito library使用 mockito 库的 Scala 单元测试
【发布时间】:2015-09-12 10:48:21
【问题描述】:

我正在 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)

我不知道怎么了。

【问题讨论】:

  • 看起来您正在导入并在测试中使用 Future 的 Scalaz 版本,但在 ImplModuleRepository 中没有。
  • 非常感谢@Shadowlands,我现在明白了,在过去的两个小时里一直陪伴着我,再次非常感谢
  • 没问题 - 有时您只需要有人用新的眼光看待同一事物。

标签: scala testing mockito akka-http


【解决方案1】:

(来自 cmets):

您在测试中似乎正在导入并使用 Future 的 Scalaz 版本,而不是在 ImplModuleRepository 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多