问题描述
我正在使用 scalatestplus-play"% "5.1.0"
,我正在尝试将 MockitoSugar
带到我的班级,就像我在早期项目中所做的那样:
I'm using "scalatestplus-play" % "5.1.0"
, and I'm trying to bring MockitoSugar
to my class like I did in earlier projects:
class MyTestSpec extends AnyFreeSpec with MockitoSugar
所以我可以使用 mock[MyClass]
函数,现在它存在,在文档中找不到其他东西.
so I can use the mock[MyClass]
function and now it docent exists, can't find in the documentation something else.
在我早期的项目中,我使用了 scalatestplus-play";% "4.0.3"
,我确定现在有一种不同的方式,但找不到它是什么
in my earlier projects I used "scalatestplus-play" % "4.0.3"
, I'm sure there is a different way now but can't find what it is
推荐答案
您的第一个选择是使用 scalatestplus-mockito,您可以在其中找到 MockitoSugar
,就像您习惯的那样.
First option you have, is to use scalatestplus-mockito, where you can find MockitoSugar
, as you are used to.
来自 ScalaTest 使用模拟对象进行测试:
From ScalaTest Testing with mock objects:
您可以将任何 Java 模拟框架与 ScalaTest 或 ScalaMock(一种 Scala 模拟替代方案)结合使用.ScalaTest 为三种最流行的 Java 模拟框架(JMock、EasyMock 和 Mockito)提供了足够的语法糖,以去除样板并澄清客户端代码.
另一种选择,如附加链接中所述,有 4 个库可以使用.我认为最简单的一种是 ScalaMock
.要使用它,您需要扩展 MockFactory
.
Another option, as said in the attached link, there are 4 libraries that can be used. The easiest one in my opinion is ScalaMock
. To use it you need to extend MockFactory
.
它应该从:
import org.scalamock.scalatest.MockFactory
不同之处在于 scalatestplus-play"%4.0.3"
取决于 ScalaTest 3.0.8 版仍然有 MockitoSugar
.scalatestplus-play"%5.1.0"
依赖于 ScalaTest 3.1.1 不再拥有它.
The difference is that "scalatestplus-play" % "4.0.3"
depends on ScalaTest version 3.0.8 which still had MockitoSugar
. "scalatestplus-play" % "5.1.0"
depends on ScalaTest 3.1.1 which no longer has it.
这篇关于不推荐使用 scalatestplus-play 的 MockitoSugar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!