【发布时间】:2022-09-23 04:34:55
【问题描述】:
我们需要使用 mock-maker-inline 来模拟一些第三方库(例如 Azure SDK)的最终类。
我们使用以下版本的 scalatest 和 mockito:
scalaVersion := \"2.12.2\"
val ScalaTestVersion = \"3.2.5\"
val ScalaCheckVersion = \"1.14.2\"
val MockitoVersion = \"3.4.0\"
val DockerItVersion = \"0.9.9\"
val MockJavaMailVersion = \"1.9\"
val MockitoScalaVersion = \"1.1.4\"
val ScalaPlusScalaCheckVersion = \"3.2.2.0\"
val ScalaPlusMockitoVersion = \"3.2.10.0\"
lazy val MockitoIssueSample = (project in file(\".\"))
.settings(
name := \"MockitoIssueSample\",
libraryDependencies += \"org.scalatest\" %% \"scalatest\" % ScalaTestVersion % Test,
libraryDependencies += \"org.scalacheck\" %% \"scalacheck\" % ScalaCheckVersion % Test,
libraryDependencies += \"org.mockito\" % \"mockito-core\" % MockitoVersion % Test,
libraryDependencies += \"org.mockito\" %% \"mockito-scala\" % MockitoScalaVersion % Test,
libraryDependencies += \"org.scalatestplus\" %% \"scalacheck-1-14\" % ScalaPlusScalaCheckVersion % Test,
libraryDependencies += \"org.scalatestplus\" %% \"mockito-3-4\" % ScalaPlusMockitoVersion % Test,
)
在我们的 Scala 应用程序中启用 mock-maker-inline 后,其他使用具有 final 方法的 trait 的测试用例开始失败,并出现以下错误:
[info] - should should invoke area of the appropriate shape *** FAILED ***
[info] org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected.
[info] Clean & maintainable test code requires zero unnecessary code.
[info] Following stubbings are unnecessary (click to navigate to relevant line of code):
[info] 1. -> at cortex.mockito.sample.AreaCalculatorSpec$$anon$1.<init>(AreaCalculatorSpec.scala:27)
[info] Please remove unnecessary stubbings or use \'lenient\' strictness. More info: javadoc for UnnecessaryStubbingException class.
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[info] at scala.Option.fold(Option.scala:158)
[info] at cortex.mockito.sample.AreaCalculatorSpec.withFixture(AreaCalculatorSpec.scala:13)
[info] at org.scalatest.wordspec.AnyWordSpecLike.invokeWithFixture$1(AnyWordSpecLike.scala:1075)
[info] at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTest$1(AnyWordSpecLike.scala:1087)
[info] at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
[info] at org.scalatest.wordspec.AnyWordSpecLike.runTest(AnyWordSpecLike.scala:1087)
[info] at org.scalatest.wordspec.AnyWordSpecLike.runTest$(AnyWordSpecLike.scala:1069)
[info] at org.scalatest.wordspec.AnyWordSpec.runTest(AnyWordSpec.scala:1879)
[info] at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTests$1(AnyWordSpecLike.scala:1146)
[info] ...
我们已经用测试 Scala 应用程序模拟了这个问题。如果我们禁用 mock-maker-inline 那么这个测试用例可以工作。在这里,在这个示例应用程序中,我们只添加了一个有问题的测试用例。
下面是示例代码:
-
形状.scala
包 mockito.sample
特征形状{
最终定义打印区域():单位= { println(s\"区域为:$getArea()\") }
def getArea(): 双倍
}
-
矩形.scala
包 mockito.sample
类矩形(l:长,b:长)扩展形状{
覆盖 def getArea(): Double = { 磅 }
}
-
面积计算器.scala
包 mockito.sample
类面积计算器(形状:形状){
def printArea(): 布尔 = { 形状.printArea() 真的 }
}
-
AreaCalculatorSpec.scala
包 mockito.sample
导入 org.mockito.integrations.scalatest.IdiomaticMockitoFixture
导入 org.scalatest.concurrent.ScalaFutures
导入 org.scalatest.matchers.must.Matchers.convertToAnyMustWrapper
导入 org.scalatest.matchers.should.Matchers
导入 org.scalatest.wordspec.AnyWordSpec
导入 org.scalatest.{EitherValues, TryValues}
导入 org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
类 AreaCalculatorSpec 扩展 AnyWordSpec 与匹配器 使用 ScalaFutures 使用 EitherValues 使用 TryValues 使用 IdiomaticMockitoFixture 使用 ScalaCheckPropertyChecks {
特征设置{ val 矩形 = 模拟 [矩形] val areaCalculator = new AreaCalculator(矩形) }
\"AreaCalculator#printArea\" 应该 { \"应该调用适当形状的区域\" 在新设置 { rectangle.getArea() 应该返回 40.0 areaCalculator.printArea() 必须为真 } }
}
请检查并建议您的宝贵意见。让我知道是否需要任何其他详细信息。
谢谢,
拉克什·丹胡基亚
标签: scala unit-testing mockito