问题描述
我是在 Scala 中编写 junit 测试的新手,我正在使用 Mockito 来模拟对象.我也在使用 scalatest_2.12-3.0.4
.ScalaTest 文档(如此处)显示了使用 MockitoSugar 创建模拟的语法,即>
I'm new to writing junit tests in Scala and I'm using Mockito to mock objects. I'm also using scalatest_2.12-3.0.4
. The ScalaTest documentation (like here) shows the syntax to create the mock using MockitoSugar, i.e.
val mockCollaborator = mock[Collaborator]
Eclipse 在导入语句中显示 org.scalatest.mock.MockitoSugar
被划掉,表明它已被弃用.我发现的唯一替代方法是,不要使用 MockitoSugar 而是使用:
Eclipse shows org.scalatest.mock.MockitoSugar
crossed out in the import statement, indicating it is deprecated. The only alternative I've found is, don't use MockitoSugar and instead do:
val mockCollaborator = mock(classOf[Collaborator])
这似乎也很好用,所以我很好奇 ScalaTest 推荐我使用什么.
This seems to work fine too, so I'm curious what ScalaTest is recommending me to use.
或者就此而言,我为什么要使用 MockitoSugar?它还有其他功能吗?我一直找不到关于它的任何文档,除了每个人似乎都使用速记 mock[]
符号.
Or for that matter, why else would I use MockitoSugar? Does it have any other features? I've been unable to find any documentation about it, except that everybody seems to use the shorthand mock[]
notation.
推荐答案
基于 issue 和一些评论,答案应该改变:
Base on the issue and some comments, the answer should be changed:
MockitoSugar
已移至单独的项目:https://github.com/scalatest/scalatestplus-mockito- 需要添加一个新的依赖来使用它
https://mvnrepository.com/artifact/org.scalatestplus/mockito-3-4_2.13/3.3.0.0-SNAP3
testCompile group: 'org.scalatestplus', name: 'mockito-3-4_2.13', version: '3.3.0.0-SNAP3'
起源问题:https://github.com/scalatest/scalatest/issues/1789
如source 你应该使用 org.scalatest.mockito.MockitoSugar
而不是 org.scalatest.mock.MockitoSugar
As reported in the source you should use org.scalatest.mockito.MockitoSugar
instead of org.scalatest.mock.MockitoSugar
这篇关于为什么不推荐使用 scalatest MockitoSugar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!